• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Numberformat exception  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having this error when going to the menu of remove or delete can anyone help where is the problem?

Exception in thread "main" java.lang.NumberFormatException: For input string: "S8470017G"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.<init>(Integer.java:660)
at Travel.compareTo(Travel.java:33)
at Travel.compareTo(Travel.java:1)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at java.util.Collections.sort(Collections.java:117)
at lab6Ex1.main(lab6Ex1.java:110)
Java Result: 1



main method





text file

S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/01/2010 | 16:28
S22209619B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/01/2010 | 16:28
S8328944B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore | 09/01/2010 | 16:28
S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/07/2007 | 16:28
S8470017G|Abhradeep Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/08/2007 | 16:28

Thanks


 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You compare two Integers in Travel.compareTo(Travel), and if you construct a new Integer object with a String, it tries to cast it to a number. But if you try to cast "S8470017G", for example, the S and G are no numbers, so you get this exception.

Try this:



This uses String.compareTo(String).
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abhradeep Banerjee wrote:I am having this error when going to the menu of remove or delete can anyone help where is the problem?

Exception in thread "main" java.lang.NumberFormatException: For input string: "S8470017G"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.<init>(Integer.java:660)
at Travel.compareTo(Travel.java:33)
at Travel.compareTo(Travel.java:1)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at java.util.Collections.sort(Collections.java:117)
at lab6Ex1.main(lab6Ex1.java:110)
Java Result: 1




Basically, you are trying to call the collections sort() method, which will uses the Comparator interface... And your compareTo() method is trying to use the Integer comparator to compare two strings... And in converting the string, "S8470017G", to a Integer, it is failing -- as that is not a valid value for an integer.

[EDIT: Beaten to the answer again... ]

Henry
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to use compareTo for Strings??
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, String has a CompareTo method, as you can see here:
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/String.html
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jan for the quick reply
 
Ranch Hand
Posts: 282
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that String is oblivious to numeric values. For example, "24" will come before "5" because the ascii value for "2" is less than the ascii value for "5".
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


instead of above code use below code, i think its work fine.(both are same)




i think evrything is right in that code, the only problem is String characters entered as input values instead of Integer values, it causes NumberFormatException
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried both ways as you have stated im still getting the error

Exception in thread "main" java.lang.NumberFormatException: For input string: "S8470017G"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.<init>(Integer.java:660)
at Travel.compareTo(Travel.java:33)
at Travel.compareTo(Travel.java:2)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at java.util.Collections.sort(Collections.java:117)
at lab6Ex1.main(lab6Ex1.java:110)
Java Result: 1

Can anyone help me please
Thanks
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As pointed out by Satish there is problem with your NRIC value. You need to parse the value from the input file and strip it to fetch only the number from that value.
Then it should work without any further change.
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you typically see letters inside of numbers? Is "123ABC456" a number? The answer is no because there are letters in it.
 
Sheriff
Posts: 21743
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Angstadt wrote:Do you typically see letters inside of numbers? Is "123ABC456" a number? The answer is no because there are letters in it.


Actually, I'd say yes, but only if you use HEX when parsing
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Actually, I'd say yes, but only if you use HEX when parsing



Oh hohoho, I've been had
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote: . . . only if you use HEX when parsing

At least you were awake when you saw that. More than I was You can parse that with any radix ≥ 13.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i tried both ways as you have stated im still getting the error



check what situations are causes NumberFormatException. see below example

i think same thing going on in this code, check once that only parse Integer values.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!