Home > Back-end >  Reading a file -- pairing a String and int value -- with multiple split lines
Reading a file -- pairing a String and int value -- with multiple split lines

Time:05-09

I am working on an exercise with the following criteria:

"The input consists of pairs of tokens where each pair begins with the type of ticket that the person bought ("coach", "firstclass", or "discount", case-sensitively) and is followed by the number of miles of the flight."

The list can be paired -- coach 1500 firstclass 2000 discount 900 coach 3500 -- and this currently works great. However, when the String and int value are split like so:

firstclass        5000  coach         1500         coach
100 firstclass

2000    discount 300

it breaks entirely. I am almost certain that it has something to do with me using this format (not full)

while(fileScanner.hasNextLine())
{
    StringTokenizer token = new StringTokenizer(fileScanner.nextLine(), " ")
    while(token.hasMoreTokens())
    {
        String ticketClass = token.nextToken().toLowerCase();
        int count = Integer.parseInt(token.nextToken());
        ...
    }
}

because it will always read the first value as a String and the second value as an integer. I am very lost on how to keep track of one or the other while going to read the next line. Any help is truly appreciated.

Similar (I think) problems:

CodePudding user response:

If you can afford to read the text file in all at once as a very long String, simply use the built-in String.split() with the regex \s , like so

String[] tokens = fileAsString.split("\s "); 

This will split the input file into tokens, assuming the tokens are separated by one or more whitespace characters (a whitespace character covers newline, space, tab, and carriage return). Even and odd tokens are ticket types and mile counts, respectively.

If you absolutely have to read in line-by-line and use StringTokenizer, a solution is to count number of tokens in the last line. If this number is odd, the first token in the current line would be of a different type of the first token in the last line. Once knowing the starting type of the current line, simply alternating types from there.

int tokenCount = 0;
boolean startingType = true; // true for String, false for integer
boolean currentType;
while(fileScanner.hasNextLine())
{
    StringTokenizer token = new StringTokenizer(fileScanner.nextLine(), " ");
    startingType = startingType ^ (tokenCount % 2 == 1); // if tokenCount is odd, the XOR ^ operator will flip the starting type of this line
    tokenCount = 0;
    while(token.hasMoreTokens())
    {
        tokenCount  ;
        currentType = startingType ^ (tokenCount % 2 == 0); // alternating between types in current line
        if (currentType) {
            String ticketClass = token.nextToken().toLowerCase();
            // do something with ticketClass here
        } else {
            int mileCount = Integer.parseInt(token.nextToken());
            // do something with mileCount here
        }
        ...
    }
}
  • Related