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

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Or is your problem reading the file and extracting the two integer values line by line?

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

    Default

    Quote Originally Posted by pbrockway2 View Post
    Or is your problem reading the file and extracting the two integer values line by line?
    ^^^ exactly. i need to extract each line from the text file and assign it to the 2D array

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

    Default

    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

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 01:07 AM.

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

    Default

    thanks for the info. i changed the name and recompiled and am only getting 1 error now on line 21

    Java Code:
    maze[j][j] = br.readline();
    error: cannot find symbok
    symbol: method readline()
    location maze [j][j] = br.readline();

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 06:25 AM
  2. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 09:33 PM
  3. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 04:27 PM
  4. how to pass array in java?
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 01-13-2008, 05:33 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
  •