Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception Errors

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

    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; December 10th, 2009 at 01:19 AM. Reason: title


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Exception Errors

    hmm... that's weird. The next() method is taking in multiple numbers at a time. What you could do is just use the Scanner.nextInt() method and skip the trouble of having to use the Integer wrapper class to convert from String to int.

    int hold = fromFile.nextInt();

Similar Threads

  1. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 01:47 PM
  2. GuessWhat- same errors repeated 4 times?
    By iank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2009, 07:32 PM
  3. Errors with LispList
    By Newoor in forum Collections and Generics
    Replies: 10
    Last Post: October 25th, 2009, 04:25 PM
  4. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM
  5. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 06:04 PM

Tags for this Thread