7 Replies - 514 Views - Last Post: 03 April 2011 - 09:16 PM Rate Topic: -----

#1 Sasquatch3o3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-April 11

problem with file input to 2D array

Posted 03 April 2011 - 07:44 PM

I've got a file that contains this
5
0
-1,0
1,-1,0
-1,1,-1,0
-1,-1,-1,1,0
I have to ignore the 5 and the put the 0,1,and -1's in a 2D array.
Here is what I have tried and still can't get it.
FileInputStream fstream  = new FileInputStream("map5.txt");
      Scanner scan = new Scanner(fstream);
      DataInputStream In = new DataInputStream(fstream);
      BufferedReader reader = new BufferedReader(new InputStreamReader(In));
		String line;
		
		
		while ((line = reader.readLine()) != null)
      {
        StringTokenizer line2 = new StringTokenizer(line, ",");
 
        while (line2.hasMoreTokens())
        {
   		int temp;
			temp = Integer.parseInt(line2.nextToken());   
			map[col][row] = temp;
         col++;
			row++;
        }
        
		 }


Is This A Good Question/Topic? 0
  • +

Replies To: problem with file input to 2D array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: problem with file input to 2D array

Posted 03 April 2011 - 08:04 PM

Don't see where you initialized row and col
Beside that where is your problem ?

probably miss a row++ and a col++ somehere

This post has been edited by pbl: 03 April 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 Sasquatch3o3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-April 11

Re: problem with file input to 2D array

Posted 03 April 2011 - 08:12 PM

Here is what I have so far, just to show that I have initialized the row and col to zero.
It gives me this error
java.lang.ArrayIndexOutOfBoundsException: 5
on my "map[col][row] = temp;" line, and I have absolutly NO idea how to solve it.
public class FindPath
{
	public static void main(String[] args) throws IOException
	{
		
		
		Scanner input = new Scanner(System.in);
		//Read the file
		File inputfile = new File("map5.txt");
		Scanner file = new Scanner (inputfile);
		
		//Get the number of cities
		int numCities;
		numCities = file.nextInt();

		
		
		
		
		
		int start, end;
		//Get the starting city
		System.out.println("What is your starting city?");
		start = input.nextInt();
		
		
		//Get the Ending city
		System.out.println("What is the ending city?");
		end = input.nextInt();
		
		
		
		

		
		
		
		
		//2D array of the map.
		 int[][] map = new int[numCities][numCities];
		
		int col, row;
		col = 0;
		row = 0;
		    	  
		FileInputStream fstream  = new FileInputStream("map5.txt");
                Scanner scan = new Scanner(fstream);
                DataInputStream In = new DataInputStream(fstream);
                BufferedReader reader = new BufferedReader(new InputStreamReader(In));
		String line;
		
		
		while ((line = reader.readLine()) != null)
      {
        StringTokenizer line2 = new StringTokenizer(line, ",");
 
        while (line2.hasMoreTokens())
        {
   		int temp;
			temp = Integer.parseInt(line2.nextToken());   
			map[col][row] = temp;
         col++;
			row++;
        }
        //row++;
		 }
		
		file.close();

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: problem with file input to 2D array

Posted 03 April 2011 - 08:33 PM

Seriously, you posted a problem
You come back with a completly different code
What should we do ? Which code is the right one ? Which one gives you problem ?
Do you think we are magician guessing which code should be fixed ?
Make up your mind and we'll see what we can do.
Was This Post Helpful? 0
  • +
  • -

#5 Sasquatch3o3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-April 11

Re: problem with file input to 2D array

Posted 03 April 2011 - 08:36 PM

Yeah, I can't fill the 2D array because it is giving me the error.
The second code is my full class.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10805
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: problem with file input to 2D array

Posted 03 April 2011 - 08:54 PM

Your variable placements are a little misleading for a 2D array. The first dimmension is the row, or 1D array; and the second dimmension is the column, or element within the 1D array.
map[col][row] = temp;  
col++;  
row++;  



Also, you should not be incrementing both variables in your inner loop. You are going diagonally across the array. After you adjust so you are reading into map[row][col], you should be incrementing col in the inner loop to go across the 1D array. Then at the end of your outer loop, you should be incrementing row to go to the next 1D array and reset col to 0, starting at the beginning of the next 1D array.

Lastly, be careful that row remains < map.length (the number of 1D arrays), and col < map[row].length, the number of elements in the 1D array.
Was This Post Helpful? 0
  • +
  • -

#7 Sasquatch3o3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-April 11

Re: problem with file input to 2D array

Posted 03 April 2011 - 09:14 PM

Alright so I did do some modifying to the code
FileInputStream fstream  = new FileInputStream("map5.txt");
      Scanner scan = new Scanner(fstream);
      DataInputStream In = new DataInputStream(fstream);
      BufferedReader reader = new BufferedReader(new InputStreamReader(In));
		String line;
		
		
		while ((line = reader.readLine()) != null)
      {
        StringTokenizer line2 = new StringTokenizer(line, ",");
 
        while (line2.hasMoreTokens())
        {
   		int temp;
			temp = Integer.parseInt(line2.nextToken());
			map[col][row] = temp;
			col++;
        }
        row++;
		  col = 0;
		 }


I made the col increment in the inner loop and when that is done I set col back to zero to go back to the start of each row, and it still gave me this error
java.lang.ArrayIndexOutOfBoundsException: 5 at my "map[col][row] = temp;" line.
map[][] size is 5x5.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10805
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: problem with file input to 2D array

Posted 03 April 2011 - 09:16 PM

Remember that arrays are indexed from 0 to length-1. So if you have the length of one of the dimmensions is 5, you can only go from 0-4 for that dimmension. See my last post regarding checking the row and col variables against the corresponding lengths.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1