java 使用 Scanner 类打印字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39004805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print string by using Scanner class
提问by Hassan Raza
I am giving this input "Welcome to HackerRank's Java tutorials!" into but
我给出了这个输入“欢迎使用 HackerRank 的 Java 教程!” 成但是
printing only "Welcome" string through scanner class.
通过扫描仪类仅打印“欢迎”字符串。
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.nextLine();
scan.close();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
How to resolve this?
如何解决这个问题?
采纳答案by Wiktor Stribi?ew
The point is that your string does not contain the integer and a double value on the input line.
关键是您的字符串在输入行中不包含整数和双精度值。
If you provide 12 2.56 Welcome to HackerRank's Java tutorials!
, it will work:
如果您提供12 2.56 Welcome to HackerRank's Java tutorials!
,它将起作用:
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
See the Java demo
查看Java 演示
Output:
输出:
String: Welcome to HackerRank's Java tutorials!
Double: 2.56
Int: 12
If you want to make sure your string gets parsed, check the next token with hasNext
methods (hasNextInt()
and hasNextDouble()
):
如果您想确保您的字符串被解析,请使用hasNext
方法 (hasNextInt()
和hasNextDouble()
)检查下一个标记:
Scanner scan = new Scanner(System.in);
int i = 0;
if (scan.hasNextInt())
i = scan.nextInt();
double d = 0d;
if (scan.hasNextDouble())
d = scan.nextDouble();
String s = scan.nextLine();
scan.close();
See this demo.
请参阅此演示。
回答by VED
When switching between reading tokens of input and reading a full line of input, you need to make another call to nextLine()
because the Scanner object will read the rest of the line where its previous read left off.
在读取输入标记和读取整行输入之间切换时,您需要再次调用 ,nextLine()
因为 Scanner 对象将读取前一次读取停止的行的其余部分。
If there is nothing on the line, it simply consumes the newline and moves to the beginning of the next line.
如果该行没有任何内容,它只会消耗换行符并移动到下一行的开头。
After double declaration, you have to write: scan.nextLine();
双重声明后,你必须写: scan.nextLine();
回答by komal akhani
Please write following code..It will work!!
请编写以下代码..它会工作!!
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
NOTE:If you use the nextLine() method immediately following the nextInt() or nextDouble() [reading tokens of input and reading a full line of input] method, recall that nextInt() or nextDouble() reads integer tokens; because of this, the last newline character for that line of integer or double input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer or double line.So,you need to make another call to nextLine().
注意:如果您在 nextInt() 或 nextDouble() [读取输入的标记并读取输入的整行] 方法之后立即使用 nextLine() 方法,请记住 nextInt() 或 nextDouble() 读取整数标记;因此,该整数或双输入行的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数或双行的其余部分。因此,您需要再次调用下一行()。
回答by Devang
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("\nEnter The String= ");
String s = scan.nextLine();
System.out.println("\nEnter The int= ");
int i = scan.nextInt();
System.out.println("\nEnter The Double= ");
double d = scan.nextDouble();
scan.close();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}