• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Code problem

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


I have the following program, I have to get the odd n even numbers out.

I ma getting an exception as :
Exception in therad "main" java.lang.NumberFormatException :For inputString
"The first data value is"
at OddEven.main<OddEven.java :17>



The data.in file contains the data as :
The first data value is
1066
The second data value is
1492
The third data value is
1939
The fourth data value is
1944
The fifth data value is
2000


Can you explain where am I wrong.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that is the contents of your "data.in", then you are passing in a String which cannot be parsed to a float. You need to skip a line each time because your program is catching "The first data value is" as float, when you think your getting "1066".

Its not an ideal solution, but you need to do a "inFile.readLine();" before you do your float parsing (where you would to another "indline.readLine()", and then again in the loop.
[ April 16, 2005: Message edited by: Kashif Riaz ]
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did the inFile.readLine() before the parsing the float.

inputString = inFile.readLine();
value = Float.valueOf(inputString).floatValue();
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you must read two lines: skip the first, parse the second. Do this repeatedly.

The other option is to put the conversion into a try-catch block and ignore the line if it can't be parsed into a number. Put that inside the loop reading lines. This way your code will handle a file that isn't "text-num-text-num" but rather "text-num-num-text-num" or any other combination, but only if the assignment calls for it of course.

Also, why are you parsing the numbers as float when the file clearly has integers and you're casting the float to an int before adding to the sum anyway. You lose any decimal portion that may have been there, so why parse it as float in the first place?
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic