Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numberformat exception

 
Abhradeep Banerjee
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


 
Jan Hoppmann
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).
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
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
 
Michael Angstadt
Ranch Hand
Posts: 277
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".
 
satish varma
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
 
Mithun Kulkarni
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: 277
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java 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: 277
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • 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