[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:
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:
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.