繁体   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);
}

带有第18行(打印名称); 回应:[不正确]

txtName: Delvin
name: A
txtName: Demarco
name: A
txtName: Demarcus
name: A
txtName: Demario
name: A
....

没有第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
....

代码是这样流动的:

  1. 用户输入字符串(名称)
  2. readFile被称为,我创建了一个新的Scanner对象并传递了一个new File
  3. 我希望应用程序继续运行,直到到达.txt文件的最后一行,因此,当输入具有nextLine时,它将继续循环。
  4. 我将字符串从单行传递到scanLine,然后为该字符串创建一个新的Scanner对象。
  5. 每行看起来像:字符串[名称],整数[数字],整数,整数,整数...
  6. 找到与用户输入的名称相匹配的行后,将其发送到getStats()[至此为止。

问题:由于某种原因,仅打印name变量(第18行)oneLine.next()似乎开始在.txt文件中途开始打印。

但是,如果我不打印名称,(或者根本不使用name变量)-看来效果很好,将第一个字母A打印到最后一个字母。

问题:由于某种原因,仅打印名称变量(第18行)oneLine.next()似乎开始在.txt文件中途开始打印。

无论是在Windows控制台中运行还是在IDE(例如Eclipse)中运行,输出窗口都限于其可以显示的行数。

当每行输入打印2行时,输出窗口溢出,并且输出的前半部分(?)被丢弃。 它仍然被打印,只是在您看到之前滚了下来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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