Results 1 to 10 of 10
  1. #1
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default Printing 2D Array Error

    I am making a program that will navigate a path in a maze I created with a 2D Array. I have everything mostly in place and am trying to print out the maze... everything complies fine but I get this error when I try to run it:



    Here is my code:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Maze
    {
    	int n = 7;
    
    
    public static void main (String[] args)
    	{
    		Maze m = new Maze();
    		m.printboard();
    	}
    	
    	public void printboard()
    	{
    		int n = 7;
    		for(int row = 0; row<n;row++)
    			for(int col = 0; col<n; col++){
    				System.out.printf("%d /t", maze[row][col]);
    		}
    		System.out.println();
    	}
    	
    	int maze[][] = new int[n][n];
    	
    	public Maze(){
    	
    	int count = 0,num;
    		
    		try{
    		Scanner fileScan = new Scanner(new File("numbers.txt"));
    		
    				int block = fileScan.nextInt();
    		int r=0;
    		int c=0;
    		
    		//fill array w/ -1
    		for(int row = 0; row<n;row++){
    			for(int col = 0; col<n; col++){
    				maze[row][col] = -1;
    		}
    		
    		while(fileScan.hasNext()&& count<block)
    		{
    			r = fileScan.nextInt();
    			c = fileScan.nextInt();
    			maze[r][c] = 100;
    			count++;
    		}	
    			r = fileScan.nextInt();
    			c = fileScan.nextInt();
    			maze[r][c] = 0;
    			
    			r = fileScan.nextInt();
    			c = fileScan.nextInt();
    			maze[r][c] = 1000;
    	}	
    		}
    		catch(FileNotFoundException e){
    			System.out.printf("File Not Found");
    		}
    		
    
    
    	}
    	
    }

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    When you're compiling in console you need to put your file in your java JDK map. For example mine is:
    Java Code:
    C:\javaJDK\bin.
    But the standard install location is different.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    When you're compiling in console you need to put your file in your java JDK map.
    What kind of alchemy is that? Does it also involve dragon blood and virgins?

    kind regards,

    Jos

  4. #4
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by coltragon View Post
    When you're compiling in console you need to put your file in your java JDK map. For example mine is:
    Java Code:
    C:\javaJDK\bin.
    But the standard install location is different.

    not sure what that means.

    all my other programs seem to compile and run just fine but this one is giving me this cryptic error message for some reason

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by coltragon View Post
    When you're compiling in console you need to put your file in your java JDK map. For example mine is:
    Java Code:
    C:\javaJDK\bin.
    But the standard install location is different.
    If you think that putting your code into the JDK bin directory is a good idea then please can I request you never answer another technical question on here?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noble View Post
    The error message is trying to tell you that in the constructor of that class, line 58, you're trying to read an int with your Scanner but the file doesn't contain an int (anymore).

    kind regards,

    Jos

  7. #7
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The error message is trying to tell you that in the constructor of that class, line 58, you're trying to read an int with your Scanner but the file doesn't contain an int (anymore).

    kind regards,

    Jos
    well the numbers.txt file looks like this:

    Java Code:
    6
    1 2
    1 3
    1 4
    3 4
    3 5
    3 6
    0 3
    4 6
    all ints

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    And?
    What is line 58?
    Have you tried sticking some debugging code in there to see what's happening?

  9. #9
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    And?
    What is line 58?
    Have you tried sticking some debugging code in there to see what's happening?
    line 58 is:

    r = fileScan.nextInt();

    is it maybe taking it in as a String instead of an int? how would i get around that?

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    There are three lines with that code...

    Debug!
    That's the only way you'll be certain what is going on.
    You need to learn how to do that.

    Debugging will tell you if it is the first time round, the second, the last...

Similar Threads

  1. Printing an Array
    By overlordjebus in forum Java Applets
    Replies: 9
    Last Post: 04-14-2010, 02:26 AM
  2. Problem printing an array
    By denial in forum New To Java
    Replies: 3
    Last Post: 11-01-2009, 10:09 PM
  3. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  4. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 08:57 PM
  5. printing array
    By mayhewj7 in forum New To Java
    Replies: 6
    Last Post: 02-12-2009, 02:01 PM

Posting Permissions

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