Results 1 to 6 of 6
Thread: parseInt Exception errors
- 12-10-2009, 06:49 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
[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:
However, with inputInt i get the errors: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(); } } }
Any help would be great. Thank you.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)
Last edited by TIMBERings; 12-10-2009 at 07:44 AM. Reason: title
- 12-10-2009, 08:01 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, your delimeter is "," and, you seemingly have a section of the file that goes like this
so thatJava Code:x,2 1 2,x
is read in in the "next" call and the newlines are "screwing up" the parseInt call.Java Code:2 1 2
One of the many possible problems with scanner. Try using a Reader and String's split instead.
- 12-10-2009, 08:23 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
What I need to read is this:
I've never used reader or string splits before, so I'd like to stick with scanner if possible.Java Code:2 1 2,4 3 2 2,5,7 3,6 4,7,8
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.
- 12-10-2009, 08:29 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 12-10-2009, 08:40 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
I actually ended up using the StringTokenizer procedure... It ended up working.
Thank you for your help though.
- 12-10-2009, 09:11 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
parseInt
By trefoil in forum New To JavaReplies: 4Last Post: 09-09-2009, 07:12 PM -
ParseInt question
By McChill in forum New To JavaReplies: 1Last Post: 03-09-2009, 09:34 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
parseInt() vs. intValue()
By JavaPilot in forum New To JavaReplies: 5Last Post: 02-04-2009, 08:39 AM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks