Results 1 to 8 of 8
  1. #1
    TheElder777 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default 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 07:40 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Integer.parseInt error help

    Exception in thread "main" java.lang.NumberFormatException: For input string: " -99"
    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() ).

  3. #3
    TheElder777 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default 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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Integer.parseInt error help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Integer.parseInt error help

    Quote Originally Posted by TheElder777 View Post
    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?
    Did you try it? Did it work? If not, then post the relevant code as an SSCCE with all errors or explicit definitions of misbehavior and go from there.

  6. #6
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Integer.parseInt error help

    Quote Originally Posted by DarrylBurke View Post
    If myBankAccount+= getDollar(); for every time I've seen this post.....

  7. #7
    TheElder777 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default 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;
    	}
    }

  8. #8
    TheElder777 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Integer.parseInt error help

    Quote Originally Posted by doWhile View Post
    Did you try it? Did it work? If not, then post the relevant code as an SSCCE with all errors or explicit definitions of misbehavior and go from there.
    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:
    Java Code:
    myBuff.useDelimiter(",|\\n");
    where myBuff is of type Scanner.
    To have commas and new line character as delimiters.

Similar Threads

  1. Integer parseInt(str) question
    By prasads in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 11:10 PM
  2. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 06:36 AM
  3. Integer.parseInt("5.843"); Error
    By Cemi in forum New To Java
    Replies: 3
    Last Post: 04-15-2010, 06:16 PM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •