Java 如何扫描所有包含空格中的多个字符串?
我试图一个接一个地扫描字符串,其中一些可能包含空格。由于“我的代码”目前起作用,因此只有当您键入的每个字符串都是一个单词时,它才会起作用。无论行中是否有空格,扫描连续文本行的正确方式是什么Java 如何扫描所有包含空格中的多个字符串?,java,Java,我试图一个接一个地扫描字符串,其中一些可能包含空格。由于“我的代码”目前起作用,因此只有当您键入的每个字符串都是一个单词时,它才会起作用。无论行中是否有空格,扫描连续文本行的正确方式是什么 System.out.print("Enter your first name: "); fName = scan.next(); System.out.print("Enter your last name: ");
System.out.print("Enter your first name: ");
fName = scan.next();
System.out.print("Enter your last name: ");
lName = scan.next();
System.out.print("Enter your street and house number: ");
address = scan.next();
System.out.print("Enter your city: ");
city = scan.next();
System.out.print("Enter your state: ");
state = scan.next();
System.out.print("Enter your telephone number (no spaces): ");
teleNum = scan.next();
System.out.print("Enter your zip code: ");
zip = scan.next();
我喜欢使用BufferedReader br=new BufferedReader(new InputStreamReader(System.in)),但我不确定这两种方法的优缺点/区别。如果您使用的是bufferedreader,则br.ReadLine忘了提及我也尝试了nextLine,但没有成功。。缓冲阅读器似乎是一个可行的选择。你能把你的实际代码发布到什么地方吗?大概nextLine适合我。我喜欢使用BufferedReader br=new BufferedReader(new InputStreamReader(System.in)),但我不确定这两种方法的优点/缺点/区别。如果您使用的是bufferedreader,则br.ReadLine忘了提及我也尝试了nextLine,但没有成功。。缓冲阅读器似乎是一个可行的选择。你能把你的实际代码发布到什么地方吗?大概nextLine为我工作。
String[] prompts = {
"Enter your city: ",
"Enter your state: ",
};
Scanner scanner = new Scanner(System.in);
String line = "";
for (String prompt : prompts) {
System.out.println(prompt);
if (scanner.hasNextLine()) {
line += scanner.nextLine();
}
}
System.out.println(line);