Overclock
Overclock

Reputation: 21

Copied this exact code from my book and does not work

I copied this exact code from my textbook, and when I try to run it it does nothing but load, the file is in the same location of the java file, and the name is correct. Im using Dr. Java. So im just wondering why it wont run and just keeps loading. The book I am using is Java Illuminated 3rd edition. Also, the newscores.txt file just has 10 numbers, seperated by spaces.

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

public class TestScoresAndSummaryStatistics {
    public static void main(String[] args) throws IOException {

        int number;

        File inputFile = new File("newscores.txt");
        Scanner scan = new Scanner(inputFile);

        while (scan.hasNext()); {
            number = scan.nextInt();
            System.out.println(number);

        }
        System.out.println("End of file.");
    }
}

Upvotes: 0

Views: 57

Answers (1)

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9880

you have a semicolon end of while statement .you should remove it.because of this semicolon your while loop run repeatedly and your code inside while loop become separate block from the loop.

while (scan.hasNext()); {
    number = scan.nextInt();
    System.out.println(number);

}

change to

while (scan.hasNext()) {
    number = scan.nextInt();
    System.out.println(number);

}

Upvotes: 3

Related Questions