扫描仪不会读完整个句子
我正在编写一个程序,允许用户输入他的数据然后输出它。 它的3/4是正确的,但是当它到达输出地址时它只打印一个字,只能说’Archbishop’来自’Archbishop Street’。 我该如何解决?
import java.util.*; class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println("Enter Your Name: "); String name = s.next(); System.out.println("Enter Your Age: "); int age = s.nextInt(); System.out.println("Enter Your E-mail: "); String email = s.next(); System.out.println("Enter Your Address: "); String address = s.next(); System.out.println("Name: "+name); System.out.println("Age: "+age); System.out.println("E-mail: "+email); System.out.println("Address: "+address); } }
这种方法是有效的,但我不知道如何,任何人都可以解释,它是如何工作的..
String s = sc.next(); s += sc.nextLine();
我会使用Scanner#nextLine
而不是next
来获取你的address
属性。
此方法返回当前行的其余部分,不包括末尾的任何行分隔符。
由于这会返回整行,由行分隔符分隔,它将允许用户输入任何地址而不受任何约束。
以这种方式初始化扫描仪,以便使用换行符分隔输入。
Scanner sc = new Scanner(System.in).useDelimiter("\\n");
有关更多详细信息,请参阅JavaDoc
使用sc.next()获取String中的整行
Scanner的默认分隔符是空格。 检查javadoc以了解如何更改此设置。
而不是直接使用System.in
和System.out
,使用Console
类 – 它允许您在一次调用中显示提示并读取整行(从而解决您的问题)输入:
String address = System.console().readLine("Enter your Address: ");
String s="Hi"; String s1=""; //For Reading Line by hasNext() of scanner while(scan.hasNext()){ s1 = scan.nextLine(); } System.out.println(s+s1); /*This Worked Fine for me for reading Entire Line using Scanner*/
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=""; while(scan.hasNext()) { s=scan.nextLine(); } System.out.println("String: " +s); System.out.println("Double: " + d); System.out.println("Int: " + i); } }
我们可以使用scan.nextLine轻松完成。它将读取剩余的输入直到结束。 然后将其分配给您的变量。 整个句子可以轻松打印。 以下是您更好理解的示例。
String s = "HackerRank "; Scanner scan = new Scanner(System.in); String s2; scan.nextLine(); // read the rest of the line of input (newline character after the double token). s2 = scan.nextLine(); /* Concatenate and print the String variables on a new line integer variables on a new line; System.out.println(s + s2); scan.close();
}}
要获得整个地址添加
s.nextLine();//read the rest of the line as input
之前
System.out.println("Enter Your Address: "); String address = s.next();
我将令牌分隔符设置为换行符模式,读取下一个令牌,然后将分隔符放回原来的状态。
public static String readLine(Scanner scanner) { Pattern oldDelimiter = scanner.delimiter(); scanner.useDelimiter("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]"); String r = scanner.next(); scanner.useDelimiter(oldDelimiter); return r; }
next()只能读取输入直到空格。 它无法读取由空格分隔的两个单词。 此外,next()在读取输入后将光标放在同一行。
nextLine()读取包含单词之间空格的输入(即,它读取直到行尾\ n)。 读取输入后,nextLine()将光标定位在下一行。
读取整行,你可以使用nextLine()
我尝试了下面的代码,但这并没有停止,因为没有中断条件所以它总是在等待输入,可能你可以添加一个条件进行中断
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=""; while(scan.hasNext()) { s=scan.nextLine(); } System.out.println("String: " +s); System.out.println("Double: " + d); System.out.println("Int: " + i); } }
“它只打印一个字,让我们说’大主教’来自’大主教街’
它只打印这个单词,因为扫描程序的function如next(),nextInt()等只能在时间读取一个标记。 因此,此函数读取并返回下一个标记。
For example if you have: xyz s.next() will return x s.nextLine() yz
如果你想阅读整行“Archbishop Street”,请回到你的代码
String address = s.next(); // s = "Archbishop" Then address += s.nextLine(); // s = s + " Street"
下面是使用Java Scanner类读取标准输入行的示例代码。
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); System.out.println(s); scan.close(); } }
输入:
你好,世界
输出:
你好,世界
下面的代码应该做yoiu正在寻找的:
public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); System.out.println(s); scan.close(); }
你应该尝试这个代码:
scan.nextLine(); String address = s.nextLine();
这完全有效。
我有同样的问题。 这应该适合你:
s.nextLine();
你也可以使用
String address = s.nextLine(); address = s.nextLine();
使用nextLine()
而不是next()
将句子作为字符串输入。
Scanner sc = new Scanner(System.in); String s = sc.nextLine();
你可以这样做:
class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println("Enter Your Name: "); String name = s.nextLine(); System.out.println("Enter Your Age: "); String age = s.nextLine(); System.out.println("Enter Your E-mail: "); String email = s.nextLine(); System.out.println("Enter Your Address: "); String address = s.nextLine(); System.out.println("Name: "+name); System.out.println("Age: "+age); System.out.println("E-mail: "+email); System.out.println("Address: "+address); } }