• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException  RSS feed

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir,
I want to take input from a file and the file value is double. Here is the file graph1 contains-

And my Java program is-



But when i run this file it throws NumberFormatException.Why this error generate?
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but I think \\s will not split the contents based on \n. So first split the content on \n and then on \\s. But I am not sure about this...
 
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the lines are retrieved from file , space has to be added after each line.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does not sound like it is a question that is about the SCJP exam. Please take the time to carefully choose one forum before you post a question.

I'm moving this to a more appropriate forum for you.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you get the token with leading spaces?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that using "\\s" as a delimiter will not split the Strings correctly? The details of what "\\s" means are in the Pattern class.
Why are you using the old-fashioned way to read from files; the Scanner class is a lot easier?
I know many books print lots of code in the main method, but running all that code from the main method and using static fields doesn't look like good object-oriented programming to me. And please use [] rather than ... in the main method declaration.

Take a pencil and paper and write down what your String st will look like after the first line, then add the next line to it. Remember the "readLine()" method stops at the line end character. Then when you have seen what it looks like, you will understand why you are getting an Exception. And please give us full details; there is an important hint in the message which goes with the Exception. That is why Djonatah Stiegler saw what was going wrong and asked about spaces.
I think using a Scanner and its methods (it has lots of nextXXX methods and hasNextXXX methods, and you are sure to find one to suit your requirements) will make things easier.

Have you got the structure of the file right. Shouldn't the 0.00 be x[0] and the 1.00 on the same line be y[0]? In which case your for loop which only counts up to length / 2 will need to be changed.

Good luck with it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!