0

I'm writing a program that reads sports data from a text file. Each line has strings and ints mixed together, and I'm trying to read just the scores of the teams. However, even though the lines have ints, the program immediately goes to the else statement without printing out the scores. I have the two input2.nextLine() statements so that it skips two header lines that have no scores. How can I fix this?

Here's the code:

public static void numGamesHTWon(String fileName)throws FileNotFoundException{
    System.out.print("Number of games the home team won: ");
    File statsFile = new File(fileName);
    Scanner input2 = new Scanner(statsFile);


    input2.nextLine();
    input2.nextLine();

    while (input2.hasNextLine()) {
        String line = input2.nextLine();
        Scanner lineScan = new Scanner(line);
        if(lineScan.hasNextInt()){

            System.out.println(lineScan.nextInt());
            line = input2.nextLine();

        }else{
            line = input2.nextLine();



        }
    }
}

Here is the top of the text file:

NCAA Women's Basketball
2011 - 2012
2007-11-11 Rice 63 @Winthrop 54 O1
2007-11-11 @S Dakota St 93 UC Riverside 90 O2
2007-11-11 @Texas 92 Missouri St 55
2007-11-11 Tennessee 76 Chattanooga 56
2007-11-11 Mississippi St 76 Centenary 57
2007-11-11 ETSU 75 Delaware St 72 O1 Preseason NIT
fabian
  • 80,457
  • 12
  • 86
  • 114
Hector
  • 51
  • 7
  • Sorry but what output you are trying to generate exactly and what goes wrong? – Pshemo Oct 23 '15 at 19:12
  • At this point, I'm just trying to read the integers from each line, and I'm trying to output them to the console just to check that the integers are indeed being read by the scanner, however, they're not because the program automatically goes to the else statement and stops. – Hector Oct 23 '15 at 19:15
  • 1
    What do you think `lineScan.hasNextInt()` should return and why do you think so? Which part of documentation of `hasNextInt` method makes you think that your assumption is correct? – Pshemo Oct 23 '15 at 19:19
  • lineScan.hasNextInt() should return the scores, for example, it should be 63 and 54 for the first game. – Hector Oct 23 '15 at 19:21
  • correct, hence i suggest read the file line by line, then split and then parse as needed – AbtPst Oct 23 '15 at 19:21

1 Answers1

0

Method hasNextInt() try to check immediate string is int ? . So that condition is not working.

public static void numGamesHTWon(String fileName) throws FileNotFoundException {
        System.out.print("Number of games the home team won: ");
        File statsFile = new File(fileName);
        Scanner input2 = new Scanner(statsFile);


        input2.nextLine();
        input2.nextLine();

        while (input2.hasNextLine()) {
            String line = input2.nextLine();
            Scanner lineScan = new Scanner(line);

            while (lineScan.hasNext()) {
                if(lineScan.hasNextInt()) {
                    System.out.println(lineScan.nextInt()); 
                    break;
                }
                lineScan.next();
            }
            line = input2.nextLine();
        }
}

Please try this code.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26