提问人:AnnieOK 提问时间:3/17/2014 最后编辑:JSON C11AnnieOK 更新时间:4/11/2022 访问量:338830
Scanner 类中的 next() 和 nextLine() 方法有什么区别?
What's the difference between next() and nextLine() methods from Scanner class?
问:
和 之间的主要区别是什么?
我的主要目标是使用可能“连接”到任何来源(例如文件)的文本来阅读所有文本。next()
nextLine()
Scanner
我应该选择哪一个,为什么?
答:
next()
只能读取输入,直到空格。它无法读取由空格分隔的两个单词。此外,在读取输入后将光标放在同一行中。next()
nextLine()
读取输入,包括单词之间的空格(即,它读取到行尾)。读取输入后,将光标定位在下一行。\n
nextLine()
要阅读整行,您可以使用 .nextLine()
评论
来自 javadocs
next() 如果下一个标记与从指定字符串构造的模式匹配,则返回下一个标记。 nextLine() 将此扫描程序推进到当前行之前,并返回跳过的输入。
您选择哪一个取决于哪个最适合您的需求。如果是我读取整个文件,我会选择 nextLine,直到我拥有所有文件。
从 Scanner 的文档中:
扫描程序使用分隔符模式将其输入分解为标记,默认情况下,该模式与空格匹配。
从 next() 的文档中:
完整标记的前面和后面是与分隔符模式匹配的输入。
来自 JavaDoc:
- A 使用分隔符模式将其输入分解为标记,默认情况下,该模式与空格匹配。
Scanner
next()
:查找并返回此扫描程序中的下一个完整令牌。nextLine()
:将此扫描程序推进到当前行之前,并返回跳过的输入。
所以在应该返回“小”的情况下,应该返回“小例子”"small example<eol>text"
next()
nextLine()
评论
next()
\n
我总是更喜欢读取输入,然后解析字符串。nextLine()
using 将仅返回分隔符之前的内容(默认为空格)。 返回当前行后自动向下移动扫描仪。next()
nextLine()
解析数据的一个有用工具是 。nextLine()
str.split("\\s+")
String data = scanner.nextLine();
String[] pieces = data.split("\\s+");
// Parse the pieces
有关 Scanner 类或 String 类的详细信息,请参阅以下链接。
扫描仪:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
字符串:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
评论
BufferedReader
Scanner
next() 和 nextLine() 方法与 Scanner 相关联,用于获取 String 输入。它们的区别是......
next() 只能读取输入,直到空格。它无法读取由空格分隔的两个单词。此外,next() 在读取输入后将光标放在同一行中。
nextLine() 读取输入,包括单词之间的空格(也就是说,它读取到行 \n 的末尾)。读取输入后,nextLine() 将光标定位在下一行。
import java.util.Scanner;
public class temp
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter string for c");
String c=sc.next();
System.out.println("c is "+c);
System.out.println("enter string for d");
String d=sc.next();
System.out.println("d is "+d);
}
}
输出:
输入 C 的字符串
ABC def
c 是 ABC
输入 d 的字符串
d 是 def
如果你使用 nextLine() 而不是 next(),那么
输出:
输入 C 的字符串
ABC DEF c 是 d 的 ABC DEF
输入字符串
GHI d 是 GHI
我注意到除了 next() 扫描空间之外,nextLine() 扫描整行是 next 等待直到它获得一个完整的令牌,而 nextLine() 不等待完整的令牌,当获得“\n”时(即当您按 Enter 键时),扫描仪光标移动到下一行并返回跳过的上一行。它不会检查您是否给出了完整的输入,即使它会接受一个空字符串,而 next() 不接受空字符串
public class ScannerTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
String []str = new String[cases];
for(int i=0;i<cases;i++){
str[i]=sc.next();
}
}
}
通过更改 for 循环中的 next() 和 nextLine() 来尝试此程序,继续按“\n”即没有任何输入的回车键,您会发现使用 nextLine() 方法它会在按下给定数量的案例后终止,其中 next() 不会终止,直到您为给定数量的情况提供并输入它。
评论
next()
nexLine()
System.out.println(str[i]);
str[i]=sc.next();
str[i]=sc.nextLine();
简而言之:如果你输入一个长度为 t 的字符串数组,那么 Scanner#nextLine() 需要 t 行,字符串数组中的每个条目都通过回车键与其他条目区分开来。Scanner#next() 会一直接受输入,直到你按回车键,但将 string(word) 存储在数组中,数组用空格分隔。
让我们看一下以下代码片段
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String[] s = new String[t];
for (int i = 0; i < t; i++) {
s[i] = in.next();
}
当我在IDE中运行上述代码片段时(假设字符串长度为2),是否将字符串输入为无关紧要
输入为 :- abcd abcd 或
输入为 :-
ABCD银行
ABCD银行
输出将像 ABCD银行
ABCD银行
但是,如果在同一代码中,我们将 next() 方法替换为 nextLine()
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String[] s = new String[t];
for (int i = 0; i < t; i++) {
s[i] = in.nextLine();
}
然后,如果您在提示时输入输入为 - ABCD ABCD
输出为 :-
ABCD ABCD
如果您在提示时输入输入为 ABCD(如果您按 Enter 键在另一行中输入下一个 ABCD,输入提示将退出,您将获得输出)
输出为:-
ABCD银行
Scanner.next() 和 nextLine() 的另一个例子如下: nextLine() 不允许用户键入,而 next() 使 Scanner 等待并读取输入。
Scanner sc = new Scanner(System.in); do { System.out.println("The values on dice are :"); for(int i = 0; i < n; i++) { System.out.println(ran.nextInt(6) + 1); } System.out.println("Continue : yes or no"); } while(sc.next().equals("yes")); // while(sc.nextLine().equals("yes"));
关键点是找到调用方法后方法将停止的位置以及光标将位于的位置。
所有方法都将读取游标位置和下一个默认分隔符(空格、制表符、\n--按 Enter 创建)之间不包含空格的信息。游标在分隔符之前停止,但 除外,它读取游标位置和 \n 之间的信息(包括由分隔符创建的空格),并且游标停在 \n 后面。nextLine()
例如,请考虑下图:
|23_24_25_26_27\n
|
- >当前光标位置
_
->空格
stream -> 粗体(调用方法获取的信息)
看看调用这些方法时会发生什么情况:
nextInt()
阅读 23|_24_25_26_27\n
nextDouble()
阅读 23_24|_25_26_27\n
next()
阅读 23_24_25|_26_27\n
nextLine()
阅读 23_24_25_26_27\n|
在此之后,应根据您的要求调用该方法。
评论
扫描程序使用分隔符模式将其输入分解为标记,默认情况下称为空格。
Next() 用于读取单个单词,当它获得空格时,它会停止读取并将光标返回其原始位置。NextLine(),而这个即使遇到空格也会读取整个单词。光标在完成读取时停止,光标回到行尾。 所以当你想把一个完整的单词读成一个句子时,你不需要使用分隔符,你只需要使用NextLine()。
public static void main(String[] args) {
// TODO code application logic here
String str;
Scanner input = new Scanner( System.in );
str=input.nextLine();
System.out.println(str);
}
我还遇到了一个关于分隔符的问题。 问题完全是关于输入的
- 输入您的姓名。
- 输入您的年龄。
- 输入您的电子邮件。
- 输入您的地址。
问题
- 我成功地完成了姓名、年龄和电子邮件。
- 当我想出两个带有空格的单词(Harnet street)的地址时,我只得到了第一个“harnet”。
解决方案
我使用扫描仪的分隔符并成功了。
例
public static void main (String args[]){
//Initialize the Scanner this way so that it delimits input using a new line character.
Scanner s = new Scanner(System.in).useDelimiter("\n");
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);
}
基本区别在于 next() 用于获取输入,直到遇到分隔符(默认情况下它是空格,但您也可以更改它)并返回您输入的令牌。然后,光标将保留在同一行上。而在 nextLine() 中,它会扫描输入,直到我们按下回车按钮并返回整个内容并将光标放在下一行。 **
Scanner sc=new Scanner(System.in);
String s[]=new String[2];
for(int i=0;i<2;i++){
s[i]=sc.next();
}
for(int j=0;j<2;j++)
{
System.out.println("The string at position "+j+ " is "+s[j]);
}
**
尝试通过将 Input 设置为“Hello World”来运行此代码。扫描仪读取输入直到“o”,然后分隔符 s[0] occurs.so 将是“Hello”,光标将指向分隔符后的下一个位置(在我们的例子中是“W”),当读取 s[1] 时,它会扫描“世界”并将其返回给 s[1] 作为下一个完整的标记(根据 Scanner 的定义)。如果我们改用 nextLine(),它将完整地读取“Hello World”,直到我们点击回车按钮并将其存储在 s[0] 中。 我们也可以通过使用 nextLine() 来给出另一个字符串。我建议您尝试使用此示例以及更多内容,并要求进行任何澄清。
这两个函数都用于移动到下一个扫描程序令牌。
区别在于扫描程序令牌的生成方式
next() 使用分隔符作为空格生成扫描器令牌
nextLine() 使用分隔符作为 '\n' 生成扫描器令牌(即 Enter 按键)
通过下面的代码及其输出,差异可以非常明显。
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
List<String> arrayList2 = new ArrayList<>();
Scanner input = new Scanner(System.in);
String product = input.next();
while(!product.equalsIgnoreCase("q")) {
arrayList.add(product);
product = input.next();
}
System.out.println("You wrote the following products \n");
for (String naam : arrayList) {
System.out.println(naam);
}
product = input.nextLine();
System.out.println("Enter a product");
while (!product.equalsIgnoreCase("q")) {
arrayList2.add(product);
System.out.println("Enter a product");
product = input.nextLine();
}
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("You wrote the following products \n");
for (String naam : arrayList2) {
System.out.println(naam);
}
}
输出:
Enter a product
aaa aaa
Enter a product
Enter a product
bb
Enter a product
ccc cccc ccccc
Enter a product
Enter a product
Enter a product
q
You wrote the following products
aaa
aaa
bb
ccc
cccc
ccccc
Enter a product
Enter a product
aaaa aaaa aaaa
Enter a product
bb
Enter a product
q
You wrote the following products
aaaa aaaa aaaa
bb
很明显,默认的分隔符空间是在使用 next 时将用空格分隔的产品添加到列表中,因此每次在一行上输入以空格分隔的字符串时,它们都是不同的字符串。 使用 nextLine 时,空格没有意义,整行都是一个字符串。
评论