简体   繁体   English

使用扫描仪时逻辑错误。 文本文件的第一行未打印到标准输出

[英]Wrong logic in using scanner. The first line of the text file is not printed out to stdout

I have the following code and an input file that has all numbers such that each line in the txt file has only one number. 我有以下代码和一个具有所有数字的输入文件,因此txt文件中的每一行只有一个数字。 I print each number on each line onto standard out and I stop printing if I encounter the number 42. But the problem is my scanner object which i'm using to read the file is not displaying the first number and is only printing from the second number of my txt file. 我将每行上的每个数字打印到标准输出上,如果遇到数字42,则停止打印。但是问题是我用来读取文件的扫描仪对象未显示第一个数字,而仅从第二个开始打印我的txt文件编号。 I think this has something to do with the scanner.nextline function I don't know but I wish scanner had a getcurrent or something like that to make things simpler. 我认为这与我不知道的scan.nextline函数有关,但我希望扫描器具有getcurrent或类似的东西来简化事情。 Anyway could anyone please tell me how to solve this problem and get the first line to display. 无论如何,任何人都可以告诉我如何解决此问题并显示第一行。

Here's my code: 这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class driver {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub


        File theFile = new File("filepath.../input.txt");

        Scanner theScanner = new Scanner(theFile);

        //so that the scanner always starts from the first line of the document.
        theScanner.reset();


        while(theScanner.hasNext())
        {
            if(theScanner.nextInt() == 42)
            {
                break;
            }

            System.out.println(theScanner.nextInt());

        }
    }

}

The problem is you are reading in a number when you do your check, then reading in another new number which you print out. 问题是您在检查时正在读一个数字,然后再读一个打印出的新数字。 This means you are printing out every second number. 这意味着您将每隔两个数字打印一次。 To solve it just store the number first: 要解决它,只需先存储数字:

       int number = theScanner.nextInt() 
       if(number == 42)
        {
            break;
        }

        System.out.println(number);

I was calling the nextInt() method twice on the scanner object before I printed out to standard output. 在打印到标准输出之前,我两次在扫描程序对象上调用nextInt()方法。 Once in the if statement and again in the System.out.println. 一次在if语句中,再一次在System.out.println中。 Hence the scanner started printing from the second line of the txt file. 因此,扫描仪从txt文件的第二行开始打印。

But the solution would be including a line of code like the following: 但是解决方案将包括如下代码行:

 int temp = theScanner.nextInt();

before the if statement and then modifying the if statement to : 在if语句之前,然后将if语句修改为:

if(temp == 42)
   { 
      break;

   }

   System.out.println(temp);

notice how many times have you called nextInt() method. 注意您调用了nextInt()方法有多少次。 ie twice, so your code must be skipping every other integer from your file. 即两次,因此您的代码必须跳过文件中的所有其他整数。 (only first number if you are reading only two integers from the file) (如果仅从文件读取两个整数,则仅第一个数字)

So the easiest solution is to store the integer in a local variable and use it to compare and print. 因此,最简单的解决方案是将整数存储在局部变量中,然后使用它进行比较和打印。

ie: 即:

   while(theScanner.hasNext())
        {
            int nextInteger=theScanner.nextInt();
            if(nextInteger == 42)
            {
                break;
            }

            System.out.println(nextInteger);

        }
        theScanner.close();

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

相关问题 使用 Scanner 的 java.io.FileNotFoundException(未找到文件)。 我的代码有什么问题? - java.io.FileNotFoundException (File not found) using Scanner. What's wrong in my code? 从文本文件/扫描仪存储字符串。 数组还是内置的? - Storing strings from a text file/scanner. Array or built-in? 使用扫描仪分析文件中的输入。 JAVA - Parsing input from a file using Scanner. JAVA 使用扫描仪将单词的出现次数及其计数存储在文件中。(Java) - Store occurences of words in a file and their count,using Scanner.( Java ) 使用Eclipse进行Java获取无法解决扫描程序。 有人知道我做错了吗? - Using Eclipse for java getting cannot be resolved for scanner. Anyone know what I have done wrong? 将扫描仪用于多行文本文件 - Using scanner for a multi-line text file 尝试将二进制文件作为文本读取,但扫描程序在第一行停止 - Trying to read binary file as text but scanner stops at first line 您如何以与使用扫描仪阅读 .txt 文件相同的方式阅读 .PDF 文件。 这是在java for android - How do you read a .PDF file the same way you read .txt file using scanner. This is in java for android 使用扫描仪从文本文件中读取字符串,然后打印出 - Using scanner to read string from text file and then print out 使用扫描仪和文本文件在一行中获取特定字符串 - Using scanner and a text file to get a certain string in a line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM