简体   繁体   English

打印其他任何内容时,扫描仪无法正确打印

[英]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: 代码是这样流动的:

  1. User enters a String (a Name) 用户输入字符串(名称)
  2. readFile is called, I create a new Scanner object and pass in a new File . readFile被称为,我创建了一个新的Scanner对象并传递了一个new File
  3. I want the application to keep running until it gets to the last line of the .txt file, so while the input has a nextLine, It'll keep looping. 我希望应用程序继续运行,直到到达.txt文件的最后一行,因此,当输入具有nextLine时,它将继续循环。
  4. I pass in the string from a single row to scanLine, and I create a new Scanner object for that String. 我将字符串从单行传递到scanLine,然后为该字符串创建一个新的Scanner对象。
  5. Each line looks like: String [Name], Int [Number], Int, Int, Int ... 每行看起来像:字符串[名称],整数[数字],整数,整数,整数...
  6. After it finds the line which matches with the name name the user entered, It'd then send that line to getStats() [That's it so far.] 找到与用户输入的名称相匹配的行后,将其发送到getStats()[至此为止。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM