2

I'm writing a program that reads data from a text file with various basketball sports statistics. Each line (after the two header lines) corresponds to one particular game and the scores of each team, with some other strings in there. I'm trying to use scanners to read the int scores of each game, store them in variables, and then compare them to determine which team won that game so that I can increment the wins later in the program. I figured out how to read all the ints in sequence, but I can't figure out how to read two ints in a line, store them as variables, compare them, and then move on to the next line/game.

Here is the relevant method:

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

    while (input1.hasNext()) {
        if (input1.hasNextInt()) {
            int x = input1.nextInt();
            System.out.print(x);   
            input1.next();
        } else {
            input1.next();
        }
    }

A few lines from 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

Hector
  • 51
  • 7

1 Answers1

1

read the file line by line. then split the line into a String[]. since you know where the scores are located on each line, you can then easily parse those values from the array and compare. can you please share a few lines form your input? then i can show you the exact code

you can try something like

String[] parts = str.split("\\D+");

where str is the line that you just read. now parts array will have all the numbers in your string. just read through the array, parse to int and make the comparison. note that the first three entries in this array would correspond to the date, so just ignore those.

for example

String[] parts = "2007-11-11 Mississippi St 76 Centenary 57".split("\\D+");

for (String g: parts)

        System.out.println(g);

prints out

2007
11
11
76
57

so now you can just take the last two values and compare

 while (input1.hasNextLine()) {
            String line = input1.nextLine();

             String[] parts = line .split("\\D+");

             int score1 = Integer.parseInt(parts[parts.length-2]);

             int score2 = Integer.parseInt(parts[parts.length-1]);

             /*now compare score1 and score2 and do whatever...*/
        }
AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • I'm not supposed to use arrays or parsing, just use scanners to read the file and lines and tokens. – Hector Oct 23 '15 at 17:44
  • 1
    apart from the date, are you sure that you can only get two ints in a line? – AbtPst Oct 23 '15 at 17:48
  • The date isn't counted as an int, the two scores are, and the O2 and O1 that appear aren't read as ints because they start with an "O", so the way I have it, it just prints out all the scores in sequence. It should only be two scores per line. – Hector Oct 23 '15 at 17:53
  • please see the latest edit. try to run the code and see if this works – AbtPst Oct 23 '15 at 18:01