Results 1 to 8 of 8
Thread: Integer.parseInt error help
- 10-21-2012, 05:18 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Integer.parseInt error help
Hello all. I have been debugging this piece of crap the whole day, with no luck. Please help me out thank you.
So, addBack takes a String, int, int, int as arguments. Im getting the .next() from a file and storing it in one of the temp variables. Then i need to change the temp variables from string to int, so I use Integer.parseInt(tempx) to convert. But i am getting the following error:
Exception in thread "main" java.lang.NumberFormatException: For input string: " -99"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:514)
at hw2.getList(hw2.java:44)
at hw2.main(hw2.java:12)
Where "-99" is the first value that is to be converted (in other words it is the value that resides in temp0)
Here is (part of) my code:
Java Code:String aName; Scanner myBuff = null; String temp0; String temp1; String temp2; while(myBuff.hasNext()){ for(int i = 0; i < 4; i++){ aName = myBuff.next(); temp0 = myBuff.next(); temp1 = myBuff.next(); temp2 = myBuff.next(); myDLL.addBack(aName, Integer.parseInt(temp0), Integer.parseInt(temp1), Integer.parseInt(temp1)); } }Last edited by TheElder777; 10-21-2012 at 06:40 AM.
- 10-21-2012, 05:41 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Integer.parseInt error help
Errors like this are worth reading closely...note the leading space for the String passed to Integer.parseInt (which will not be parsed as an int, hence the exception). The String class has methods to deal with leading/trailing whitespace (eg trim() ).Exception in thread "main" java.lang.NumberFormatException: For input string: " -99"
- 10-21-2012, 06:11 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Integer.parseInt error help
Wow, that makes a lot of sense. Thank you so much for your help and quick reply. One more question if you do not mind. I have: myBuff.useDelimiter(","); somewhere in my code, would .trim() still work?
Once again thank you
- 10-21-2012, 06:25 AM #4
Re: Integer.parseInt error help
Why do they call it rush hour when nothing moves? - Robin Williams
- 10-21-2012, 07:08 AM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
- 10-21-2012, 07:15 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
- 10-21-2012, 07:21 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Integer.parseInt error help
No, I'm having a problem taking inputs from the file. Here is my code. I will be trying to debug it myself in the mean time. Thank you.
Java Code:import java.util.*; import java.io.IOException; import java.io.FileReader; public class hw2{ public static void main(String[] args){ System.out.println("[*] Getting list..."); if(args.length == 3){ System.out.println("[!] Will read from " + args[0]); DLinkedList newList = getList(args[0]); }else{ System.out.println("[!] args[2] is empty. Will auto-read points.txt"); DLinkedList newList = getList("points.txt"); } System.out.println("[*] List obtained."); System.out.println("[*] Sorting..."); //aSort(newList, args[2]); System.out.println("[*] Sorting complete."); } public static DLinkedList getList(String fileName){ System.out.println("in getList..."); String aName; Scanner myBuff = null; String temp0; String temp1; String temp2; DLinkedList myDLL = new DLinkedList(); try{ System.out.println("in try"); myBuff = new Scanner(new FileReader(fileName)); myBuff.useDelimiter(","); myBuff.nextLine(); while(myBuff.hasNext()){ System.out.println("in while"); for(int i = 0; i < 4; i++){ System.out.println("in for"); aName = myBuff.next(); temp0 = myBuff.next(); temp1 = myBuff.next(); temp2 = myBuff.next(); System.out.println("before addBack"); //myDLL.addBack(aName, Integer.parseInt(temp0.trim()), Integer.parseInt(temp1.trim()), Integer.parseInt(temp2.trim())); System.out.println("end of for"); } System.out.println("end of while"); } myBuff.close(); System.out.println("end of try"); }catch(IOException e){e.printStackTrace();} System.out.println("end of get"); return myDLL; } }
- 10-21-2012, 09:49 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Integer.parseInt error help
Hello again. So i finally got rid of all the errors. Your comment helped a lot.
For anyone wondering how I got to fix it, I used this in my code:
where myBuff is of type Scanner.Java Code:myBuff.useDelimiter(",|\\n");
To have commas and new line character as delimiters.
Similar Threads
-
Integer parseInt(str) question
By prasads in forum New To JavaReplies: 3Last Post: 03-07-2011, 10:10 PM -
Integer.parseInt() error
By niteangell21 in forum New To JavaReplies: 4Last Post: 02-06-2011, 05:36 AM -
Integer.parseInt("5.843"); Error
By Cemi in forum New To JavaReplies: 3Last Post: 04-15-2010, 05:16 PM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM -
Problem with Integer.parseInt()
By Hevonen in forum New To JavaReplies: 2Last Post: 12-14-2008, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks