简体   繁体   中英

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. 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. 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. 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. Once in the if statement and again in the System.out.println. Hence the scanner started printing from the second line of the txt file.

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(temp == 42)
   { 
      break;

   }

   System.out.println(temp);

notice how many times have you called nextInt() method. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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