Results 1 to 8 of 8
Thread: Pass Values into 2D Array?
- 11-08-2010, 09:29 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
Pass Values into 2D Array?
So I'm trying to pass values from a txt file called numbers.txt that looks something like:
1 2
1 3
1 4
3 4
3 5
3 6
0 3
4 6
into a 2D array in my program that looks like:
int maze[][] = new int [7][7];
I'm not sure how to reference each line and how to tell the first number to go to the first part of the array and the 2nd number into the 2nd part. Any help would be much appreciated like usual :)
- 11-08-2010, 09:39 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Have a read of the Arrays section of Oracle's Tutorial. Especially the (rather brief) MultiDimArrayDemo class which refers to array elements with a syntax like names[0][2]. You can print out an array element (the way the example does) but also assign things to them:
Java Code:names[0][2] = "foo"; // assigns the string to position (0,2) in the array
----------------
Think about what the values in numbers.txt are (and say!). Are they positions of obstacles in the maze? Or something else?
- 11-08-2010, 09:40 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Or is your problem reading the file and extracting the two integer values line by line?
- 11-08-2010, 09:53 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
- 11-08-2010, 11:27 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
Trying to do something like this:
import java.io.*;
import java.util.ArrayList;
public class maze
{
public static void main (String[] args) throws IOException
{
FileInputStream in = new FileInputStream("numbers.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str;
int maze[][] = new int [7][7];
while ((str = br.readLine()) != null)
{
for (int j = 0; j<maze.length; j++){
maze[j][j] = br.readline();
}
}
in.close();
int i = choose.nextInt(maze.size()-1);
System.out.println( maze.get(i) );
}
}
but im getting errors on lines 23, 27, and 28 and they all say "cannot find symbol" and are pointing to the parts highlighted in red
- 11-09-2010, 12:04 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
When you post code put [code] at the start of the code and [/code]at the end - that way your code will appear on the forum as
Java Code:import java.io.*; import java.util.ArrayList; public class maze { public static void main (String[] args) throws IOException { FileInputStream in = new FileInputStream("numbers.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String str; int maze[][] = new int [7][7]; while ((str = br.readLine()) != null) { for (int j = 0; j<maze.length; j++){ maze[j][j] = br.readline(); } } in.close(); int i = choose.nextInt(maze.size()-1); System.out.println( maze.get(i) ); } }
It might be a good idea to rename your class Maze because Java classes begin with a capital (uppercase) letter and because you have a variable maze. The file name will have to be changed to Maze.java as well.
There is all sorts of ... stuff ... in here. Remove you aren't using and test what you are doing removing all the compile time problems as the compiler tells you about them. Work one step at a time.
With that in mind - and just dealing with just the problem of reading the file - you might end up with:
Java Code:import java.io.*; public class Maze { public static void main (String[] args) throws IOException { FileInputStream in = new FileInputStream("numbers.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); int maze[][] = new int [7][7]; String str; while ((str = br.readLine()) != null) { for (int j = 0; j<maze.length; j++){ maze[j][j] = br.readline(); } } in.close(); } }
If this generates a compiler message which you can't understand, copy and post the entire message and I'm sure someone will be able to say what it means.Last edited by pbrockway2; 11-09-2010 at 12:07 AM.
- 11-09-2010, 12:16 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
thanks for the info. i changed the name and recompiled and am only getting 1 error now on line 21
error: cannot find symbokJava Code:maze[j][j] = br.readline();
symbol: method readline()
location maze [j][j] = br.readline();
- 11-09-2010, 07:30 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
According to the API documentation the method should be readLine().
"cannot find symbol" usually means you made a typo with a name like this. The API linked to above is a good source of double checking things like method names.
Similar Threads
-
An Array of different integer values
By lithium002 in forum New To JavaReplies: 7Last Post: 12-04-2009, 05:25 AM -
Same values in an array
By hawaiifiver in forum New To JavaReplies: 3Last Post: 02-24-2009, 08:33 PM -
replacing array values
By Jononomous in forum New To JavaReplies: 1Last Post: 05-22-2008, 03:27 PM -
how to pass array in java?
By sivasayanth in forum New To JavaReplies: 3Last Post: 01-13-2008, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks