Results 1 to 6 of 6
  1. #1
    TIMBERings is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default [SOLVED]parseInt Exception errors

    I'm taking in an adjacent list and turning it into an adjacent matrix. I can run it without trying to turn input into inputInt.

    This is what I have, I know it's sloppy but here:

    Java Code:
    public class scc {
    	public static void main(String[] args) {
    		
    String fileName=args[0];		//input filename from command line
    		String newFile=fileName+".scc";		//create new file with .scc extension
    		int size=Integer.parseInt(args[1]);	//number of verticies //THIS IS FINE
    				
    		int adjMatrix[][] = new int[size][size];
    		int i=0;
    		String input;
    		
    		//----Read from file----
    		Scanner fromFile=null;
    	
    		try {           [INDENT][/INDENT]//try and catch
    			fromFile=new Scanner(new File(fileName));
    		}
    	
    		catch (FileNotFoundException e) {
    			System.err.println("Error opening the file " + fileName);
    			System.exit(0);
    		} 
    		fromFile.useDelimiter(",");
    		while(fromFile.hasNextLine()) {
    			while(fromFile.hasNext()){ 
    				input=fromFile.next();
    				int hold=Integer.parseInt(input);  //THIS IS WHERE I GET ISSUES
    				adjMatrix[i][hold-1]=1;
    			}
    			i++;
    		}
    		fromFile.close();
    		i=0;
    		//----------------------------------
    		System.out.println("\n\n");
    		for (i=0;i<50;i++) {
    			for(int j=0;j<50;j++){
    				System.out.print(adjMatrix[i][j]);
    			}
    			System.out.println();
    		}
    	
    	
    	}
    }
    However, with inputInt i get the errors:

    Java Code:
    humer015@humer015-Ubuntu:~/CSCI4041/prog2$ java scc graph.txt 8
    Exception in thread "main" java.lang.NumberFormatException: For input string: "2
    1
    2"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    	at java.lang.Integer.parseInt(Integer.java:458)
    	at java.lang.Integer.parseInt(Integer.java:499)
    	at scc.main(scc.java:33)
    Any help would be great. Thank you.
    Last edited by TIMBERings; 12-10-2009 at 07:44 AM. Reason: title

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, your delimeter is "," and, you seemingly have a section of the file that goes like this
    Java Code:
    x,2
    1
    2,x
    so that
    Java Code:
    2
    1
    2
    is read in in the "next" call and the newlines are "screwing up" the parseInt call.

    One of the many possible problems with scanner. Try using a Reader and String's split instead.

  3. #3
    TIMBERings is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    What I need to read is this:
    Java Code:
    2
    1
    2,4
    3
    2
    2,5,7
    3,6
    4,7,8
    I've never used reader or string splits before, so I'd like to stick with scanner if possible.

    When I was getting what seemed to be fairly correct output, I put commas before and after each line. Which would have been wrong anyway, I put them in to test and forgot to take them out.

    If reader and string splits are the only way, can you help me find an understandable site?

    Thank you.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    There is a first time for everything, and since scanner is obviously not working why try and force it. See the Tutorial (namely the second example), and Google.

  5. #5
    TIMBERings is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    I actually ended up using the StringTokenizer procedure... It ended up working.

    Thank you for your help though.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    StringTokenizer is even worse than Scanner, but more power to you.

Similar Threads

  1. parseInt
    By trefoil in forum New To Java
    Replies: 4
    Last Post: 09-09-2009, 07:12 PM
  2. ParseInt question
    By McChill in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 09:34 PM
  3. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  4. parseInt() vs. intValue()
    By JavaPilot in forum New To Java
    Replies: 5
    Last Post: 02-04-2009, 08:39 AM
  5. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM

Tags for this Thread

Posting Permissions

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