• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Program with methods error help  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code compiles but when ran it gives the error:

java.lang.NumberFormatException: For input string: "[-0.07114248241288923, 4.066157729530229]"
at java.lang.NumberFormatException.forInput… Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

I believe it's because of the: readTimes [i] = Integer.parseInt(readFile.readLine()); part in the readFile method, but I don't know how to fix it. What is happening?. Thanks

 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you are trying to parse an Integer from a string which represents some floating value.

Yes, I just tried out this code:


and it throws up:
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julianna

Can you post the contents of the file arrayPoints.txt?
 
Marshal
Posts: 60845
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see several problems in that code. Please go back with the button and indent it; even with code tags it is difficult to read.
The loop runs 1000 times; how do you know there are 1000 lines in the file? Why not use a while loop?
Why are you using a BufferedReader for a text file rather than a Scanner?
Your input, as shown in the stack trace, is not suitable for parsing to an int, as you have already been told. You can however create a Point class which can take that String as a constructor argument.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Julianna, Mohamed Sanaulla has stated the error in your code correctly. . . .

Although it compiles fine but still it gives a runtime exception because you are trying to store a fractional value in an integer variable.

The fix for your code is pretty simple, just change the array type to double instead of int---


and instead of converting it to int from String, convert it to double ---


And please while initializing the array readTimes dont assume its size to be 10000 because it hold up your resource space unnecessarily.
 
Campbell Ritchie
Marshal
Posts: 60845
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ritobroto Seth wrote: . . . And please while initializing the array readTimes dont assume its size to be 10000 because it hold up your resource space unnecessarily.

80000 bytes? A tiny amount of memory. It might have been a lot in 1978, but not now.,

There are other better reasons why the assumption of 10000 is poor programming, like the fact that there will be a load of 0.0s at the end of the array. Or even worse, there might be 10001 elements
There are all sorts of ways to get an array of exactly the right size.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!