[英]Scanner not printing correctly when printing anything else
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
readFile(console.next());
}
public static void readFile(String name) throws FileNotFoundException{
Scanner input = new Scanner(new File("names.txt")); // Open pipe
while(input.hasNextLine()){
String line = input.nextLine();
scanLine(line, name);
}
}
public static void scanLine(String line, String name) throws FileNotFoundException{
Scanner oneLine = new Scanner(line);
String txtName = oneLine.next();
System.out.println("txtName: " + txtName);
System.out.println("name: " + name);
}
With Line 18 (Printing name); 带有第18行(打印名称); Response: [Incorrect]
回应:[不正确]
txtName: Delvin
name: A
txtName: Demarco
name: A
txtName: Demarcus
name: A
txtName: Demario
name: A
....
Without Line 18: Response: [Correct] 没有第18行:响应:[正确]
txtName: A
txtName: Aaliyah
txtName: Aaron
txtName: Abagail
txtName: Abbey
txtName: Abbie
txtName: Abbigail
txtName: Abby
txtName: Abdiel
txtName: Abdul
txtName: Abdullah
txtName: Abe
txtName: Abel
....
This is how the code flows: 代码是这样流动的:
readFile
is called, I create a new Scanner
object and pass in a new File
. readFile
被称为,我创建了一个新的Scanner
对象并传递了一个new File
。 Problem: For some reason, just printing the name
variable (Line 18) oneLine.next() seems to start printing somewhere like half-way down the .txt file. 问题:由于某种原因,仅打印
name
变量(第18行)oneLine.next()似乎开始在.txt文件中途开始打印。
But if I don't print the name, (or don't use the name variable at all) -- It seems to work perfectly, printing the first letter A
to the last. 但是,如果我不打印名称,(或者根本不使用name变量)-看来效果很好,将第一个字母
A
打印到最后一个字母。
Problem: For some reason, just printing the name variable (Line 18) oneLine.next() seems to start printing somewhere like half-way down the .txt file.
问题:由于某种原因,仅打印名称变量(第18行)oneLine.next()似乎开始在.txt文件中途开始打印。
Whether running in a Windows console or running in an IDE (like Eclipse), the output window is limited to how many lines it can show. 无论是在Windows控制台中运行还是在IDE(例如Eclipse)中运行,输出窗口都限于其可以显示的行数。
When you print 2 lines per line of input, you're overflowing the output window, and the first half(?) of the output get discarded. 当每行输入打印2行时,输出窗口溢出,并且输出的前半部分(?)被丢弃。 It still got printed, it just scrolled off before you saw it.
它仍然被打印,只是在您看到之前滚了下来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.