wood burning stoves 2.0*
The moose likes Java in General and the fly likes Numberformat exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Numberformat exception" Watch "Numberformat exception" New topic
Author

Numberformat exception

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
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

Joined: Jul 19, 2010
Posts: 142

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).


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
is it possible to use compareTo for Strings??
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

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

Joined: Dec 09, 2009
Posts: 71
thanks Jan for the quick reply
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 272

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".


SCJP 6 || SCWCD 5
satish varma
Greenhorn

Joined: Feb 11, 2010
Posts: 27


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

Joined: Dec 09, 2009
Posts: 71
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

Joined: Jul 06, 2009
Posts: 9
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

Joined: Jun 17, 2009
Posts: 272

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

Joined: Oct 27, 2005
Posts: 19542
    
  16

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 272

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Feb 11, 2010
Posts: 27
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Numberformat exception
 
Similar Threads
deleting record
travel agent - outofmemory heap error
unable to exit program..
getting no such element exception
insertion and deletion of record from a text file