Frage

I'm using the Java tokenizer to read in values from a file which are separated by comma's (44.1, 50.6, 34.9, etc). In the file containing the values there is 12 items per line and 43 lines. It's mostly working except that it appears to only be reading the first item from a line. So instead of 516 items, I only end up with 43. Here's my method:

          BufferedReader input;
          String line;
          StringTokenizer tokenizer;

          try {

             input = new BufferedReader(new FileReader(fileName));
             while ((line = input.readLine()) != null) {

                tokenizer = new StringTokenizer(line, ",");
                double temperature = Double.parseDouble(tokenizer.nextToken());
                temps.insert(0, temperature);

             }
          }
          catch (IOException e) {
             e.printStackTrace();
             System.exit(1);
          }

Any help would be appreciated. This is my first time working with tokenizer. Sorry if this has been answered. I searched around to no avail.

War es hilfreich?

Lösung 2

Change:

double temperature = Double.parseDouble(tokenizer.nextToken());
temps.insert(0, temperature);

to:

while( tokenizer.hasMoreTokens() ) {
    double temperature = Double.parseDouble(tokenizer.nextToken());
    temps.insert(0, temperature);
}

Andere Tipps

The tokenizer grabs the first value and stops at the first ",". You need to loop through the line with:

while(tokenizer.hasMoreTokens()){

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top