문제

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.

도움이 되었습니까?

해결책 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);
}

다른 팁

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

while(tokenizer.hasMoreTokens()){

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top