The Artima Developer Community
Sponsored Link

Java Answers Forum
NumberFormatException

3 replies on 1 page. Most recent reply: Apr 30, 2007 5:15 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Jonathan Davids

Posts: 11
Nickname: jonat
Registered: Mar, 2007

NumberFormatException Posted: Apr 29, 2007 11:50 PM
Reply to this message Reply
Advertisement
Hello Friends,

Please, I have a file which I want to read and it contains some data set in this format: 12.7891 4.5678 1.1234 30.97 etc. After reading and tokenizing the file, I wanted to store it in an array as double but it gives me a NumberFormatException error. What is the problem?

Some lines of the code


while (line != null) {
StringTokenizer tokenizer = new StringTokenizer(line, "");
int numOfTokens = tokenizer.countTokens();


while (tokenizer.hasMoreTokens()) {
if (numOfTokens <= 64) {
for(int index = 0; index < Elements.length; index++) {
String matx = tokenizer.nextToken();

Elements[index] = Double.parseDouble(matx);
}
}
//System.out.println(tokenizer.nextToken());

}
line = br.readLine();
}



Thanks,

Jonat


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: NumberFormatException Posted: Apr 30, 2007 1:12 AM
Reply to this message Reply
This line appears to be the problem:
StringTokenizer tokenizer = new StringTokenizer(line, "");
Your token should be a space not an empty String:
StringTokenizer tokenizer = new StringTokenizer(line, " ");
Vince.

Jonathan Davids

Posts: 11
Nickname: jonat
Registered: Mar, 2007

Re: NumberFormatException Posted: Apr 30, 2007 1:58 AM
Reply to this message Reply
Thanks, that was a silly mistake that costed a lot of time.

Jonat

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: NumberFormatException Posted: Apr 30, 2007 5:15 AM
Reply to this message Reply
Your using an awful lot of nested loops to do something that should be simple. You might consider replacing it with something along the lines of:
int index = 0;
for (String value : line.split(" ", 64))
    Elements[index++] = Double.parseDouble(value);

Flat View: This topic has 3 replies on 1 page
Topic: NumberFormatException Previous Topic   Next Topic Topic: Repaint question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use