简体   繁体   中英

Token and line processing Java

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

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...*/
        }

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