Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By diamonddragon

Thread: Filling an array using a .txt file

  1. #1
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Filling an array using a .txt file

    Hello, I'm new to Java and am working on a small project for class.
    The project is a simplified version of PacMan using an array[8][8] as the map. I'm required to make the array by reading from a .txt file that looks like this:

    Java Code:
    Wall 1 1
    Wall 2 3
    Wall 2 4
    Wall 2 5
    Wall 3 3
    Wall 3 4
    Ghost 2 3
    Ghost 5 6
    ... And so on.

    The method I'm supposed to create has to read the text file and insert either a Wall or Ghost in its position according to the coordinates (for example, there's a wall in array[1][1])
    Now, i've managed to read from the file, but can't seem to make the walls and ghosts...
    This is the first code I tried

    Java Code:
    public static void leer() throws IOException
        {
            java.util.Scanner s = new java.util.Scanner(new File("/Users/Viktor/Documents/University/5th Trimester/Data Structure/Projects/Project 1/MetroPacman/src/inicio.txt"));
                while (s.hasNext()) 
                {
                    if (s.next()=="Wall")
                    {
                    int i = s.nextInt();
                    int j = s.nextInt();
                    Tablero[i][j]=new Wall();
                   
                    }
                    else if (s.next()=="Ghost")
                    {
                    int i = s.nextInt();
                    int j = s.nextInt();
                    Tablero[i][j]=new Ghost();
                    }
                 }
    }
    However, this isn't working as it doesn't recognize the string as an element. I've been told by a more senior student to use splits instead, and came up with this

    Java Code:
    {
                    String[] str = s.nextLine().split("\\s+");
                    int[] nums = new int[str.length -1];
                    for(int i = 0; i < nums.length; i++)
                    {
                        nums[i] = Integer.parseInt(str[i]);
                    }
                    int x = nums[0];
                    int y = nums[1];
                    Tablero[x][y]=
    But I'm stumped from there on...
    Any help would be appreciated on how to go about with this.

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    Do not compare value of String objects using ==, because it is reference comparator.
    Use equals() method instead.

    My suggestion is that You create String object that will represent first token of the row in file, say:
    String token = s.next;

    After that use if..else and decide what to do in case it is wall or ghost.

    Or, another solution is to recieve complete row from file as one token, say,
    String token = s.nextLine(); and after that You can operate on String object as You like, say, extract substring, split string etc.
    iVikD likes this.

  3. #3
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Thanks! That piece of advice did help out a lot, the program reads the .txt, makes the array and shows it, just what I needed.
    I hate to be a bother, but I gotta ask for some more advice, this time on moving the character (PacMan).
    I've got a PacMan class that has the methods MoveU(), MoveD() and so on, and a class called Player that calls these methods depending on the keyboard input received.

    This is what they look like:

    PacMan

    Java Code:
        public static int MoverD()
        {
            for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof PacMan)
                        return i-1;
                }
            }
            return 0;
        }
        
        public int getPosicionI()
        {
            for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof PacMan)
                        return i;
                }
            }
            return -1;
        }
        
         public int getPosicionJ()
         {
            for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof PacMan)
                    {
                        return j;
                    }
                }   
            }
            return -1;
         }
         
         public void setPosicion(int x, int y)
         {
             for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof PacMan)
                    {
                        Tablero[i][j]=null;
                    }
                 }
            }
              Tablero[x][y]=new PacMan();
         }
    I didn't copy all the methods, the movement in other directions follow the same pattern as the first one.

    Here's the player class

    Java Code:
    public void Movimiento() throws IOException
        {
            
            {
                BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
                String mov = read.readLine();
                if (mov.equals("w"))
                {
                PacMan.setPosicion(PacMan.MoverU());
                }
                else if (mov.equals("s"))
                {
                PacMan.MoverD();    
                }
                else if (mov.equals("a"))
                {
                PacMan.MoverL();    
                }
                else if (mov.equals("d"))
                {
                PacMan.MoverR();    
                }
                
                
            }
        }
    I think it's somewhat inefficient, but it's what I wrote off the top of my head. What I want to know is how to give the SetPosicion() the two parameters it needs, using the returns from the individual movement methods... Or any other more efficient way would work too...

    Thanks again!

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    This:
    Java Code:
    String[] str = s.nextLine().split("\\s+");
                    int[] nums = new int[str.length -1];
                    for(int i = 0; i < nums.length; i++)
                    {
                        nums[i] = Integer.parseInt(str[i]);
                    }
                    int x = nums[0];
                    int y = nums[1];
                    Tablero[x][y]=
    I don't think is necessary, since You already know one row(one line) has only two integer values, and only one string literal.
    So You could rather say just:
    Java Code:
    int x = Integer.parseInt(str[1]);
    int y = Integer.parseInt(str[2]);
    while str[0] represents string literal, "wall" or "ghost".
    -------------

    Can You explain, what MoverD() sholud do, because as I can see, it seems no logical for me, as I would expect method MoverD() to set some value, rather than just to return a value?
    Last edited by diamonddragon; 02-11-2012 at 04:11 AM.

  5. #5
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Can You explain, what MoverD() sholud do, because as I can see, it seems no logical for me, as I would expect method MoverD() to set some value, rather than just to return a value?
    You're absolutely right, I don't know what I was thinking xD
    I've changed it, this should do the trick now (i hope

    Java Code:
    public void MoverD()
        {
            for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof PacMan)
                        Tablero[i][j]=null;
                        Tablero[i-1][j]=new PacMan();
                }
            }
        }

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    I still don't get it what You want to do with that MoverD() method.

  7. #7
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by diamonddragon View Post
    I still don't get it what You want to do with that MoverD() method.
    I want to call that method when 's' is received as an input from the keyboard to then move PacMan one space down.

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by iVikD View Post
    I want to call that method when 's' is received as an input from the keyboard to then move PacMan one space down.
    Then first You sholud know PacMan currently position, and just increment/decrement that current position according to user input parameter.

  9. #9
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by diamonddragon View Post
    Then first You sholud know PacMan currently position, and just increment/decrement that current position according to user input parameter.
    Is there a way I can do that with just one method?
    I had two methods for getPosition, one for X and one for Y, and two for setPosition...
    Then again, the get methods run through the whole array looking for the PacMan element and then return it's horizontal or vertical coordinate... There must be a better way to do this, right?

  10. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by iVikD View Post
    Is there a way I can do that with just one method?
    I had two methods for getPosition, one for X and one for Y, and two for setPosition...
    Then again, the get methods run through the whole array looking for the PacMan element and then return it's horizontal or vertical coordinate... There must be a better way to do this, right?
    You have 2 dimensional board, represented with 2 dimensional array.
    When You loop through that board, and find packman, You have its position which means You have packMan x and y coordinate.
    So only one method is enough for retrieving position.

    Setting position is simple as cake, as You just have to say to board where You want to put PackMan, say, board[3][6] = "PackMan";
    "PackMan" may be represented by string literal, as above, or You could use PackMan object(say instance of Figure Class).
    In that case, board would be represented, say, as array of figure objects, where figures are PackMan, Ghost and Wall, say Figures[][] = new Figures[a][b];
    a and b represents board length, as in case of chess board a and b would be 8. (7 because array use 0 indexing).

    Is You use instances of Figures, You could for each instance set datafield position, which will represent position of figure object on the board.
    Say if You have figure PackMan(instance of Figure class), You may get its position like this:
    int[] position = figure.getposition();

    Where figure is instance of Figure class, say Figure figure = new Figure(int posX, int posY, String type) where type represents type of figure(Wall, Ghost or PackMan).

    eg:
    Java Code:
    eg. class Figure {
    private int posX;
    private int posY;
    private String type;
    
    public Figure(int posX, int posY, String type) {
    this.posX = posX;
    etc...
    }
    
    public int[] getPosition() {
    int[] position = new int[2];
    position[0] = this.posX;
    position[1] = this.posY;
    return position;
    }
    
    public void setPosition(int posX, int posY) {
    this.posX = posX;
    etc...
    }
    
    }
    etc...
    Last edited by diamonddragon; 02-11-2012 at 05:47 AM.

  11. #11
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Ok, I think I get what you mean, and I've tried adding those methods to my program, and while it runs without error, when I try to move PacMan, it simply shows me the same thing, it doesn't move at all...

  12. #12
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by iVikD View Post
    Ok, I think I get what you mean, and I've tried adding those methods to my program, and while it runs without error, when I try to move PacMan, it simply shows me the same thing, it doesn't move at all...
    Could You show some code that You produce?

  13. #13
    iVikD is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Filling an array using a .txt file

    Quote Originally Posted by diamonddragon View Post
    Could You show some code that You produce?
    Certainly, taking into account what was said here and some other advice, I was able to get the PacMan movement methods to work. Back in my Element class, the show method looks like this

    Java Code:
     public static void mostrar(){
                  PacMan p = new PacMan(6,2);
                  Tablero[PacMan.getY()][PacMan.getX()]=p;
            for (int i=0;i<Tablero.length;i++)
            {
                for (int j=0;j<Tablero.length;j++)
                {
                    if (Tablero[i][j] instanceof Fantasma)
                    {
                        System.out.print("$");
                    }
                    else if (Tablero[i][j] instanceof PacMan)
                    {
                        System.out.print("@");
                    }
                    else if (Tablero[i][j] instanceof Fruta)
                    {
                        System.out.print("+");
                    }
                    else if (Tablero[i][j] instanceof Simple)
                    {
                        System.out.print("*");
                    }
                    else if (Tablero[i][j] instanceof Pared)
                    {
                        System.out.print("#");
                    }
                    else if (Tablero[i][j] == null)
                    {
                        System.out.print(" ");
                    }
                    System.out.print(" ");
                }
                System.out.println(" ");
            }
    For some reason, it ALWAYS shows PacMan's initial position as Tablero[0][0].... Am I doing something wrong?

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2011, 11:43 PM
  2. Help with filling an array
    By MaceMan in forum New To Java
    Replies: 6
    Last Post: 03-23-2011, 04:10 PM
  3. filling an array of Address objects..?
    By hiei_yasha in forum New To Java
    Replies: 9
    Last Post: 02-21-2011, 12:25 AM
  4. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 10:29 AM
  5. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 01:43 PM

Tags for this Thread

Posting Permissions

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