0

I'm trying to print out an int[] array for this assignment I'm working on. My code is as follows:

import VacuumCleanerEnvironment.Constants;
import java.util.*;

/**
 * Agent Class
 * Student's Assignment
 */
public class Agent {
    private String action = null;
    public Agent() {

    }

    private boolean wallCheck(Set walls, int row, int col, int i){
        int[] temp = {row, col};
        if(i == 1){ // up
            temp[0]--;
        }else if(i == 2){ //right
            temp[1]++;
        }else if(i == 3){ // down
            temp[0]++;
        }else{ // left
            temp[1]--;
        }
        return walls.contains(temp);
    }

    private void addWall(Set walls, int row, int col, String dir){
        int[] temp = {row,col};
        if(dir == "up"){
            temp[0]--;
        }else if(dir == "left"){
            temp[1]--;
        }else if(dir == "right"){
            temp[1]++;
        }else if(dir == "down"){
            temp[0]++;
        }
        walls.add(temp);
    }

    /**
     * can return:
     *      "up", "right", "down", "left", "suck dirt", "stay idle"
     * @return String
     */
    public String action(boolean dirty, boolean bump,int row, int col){
        //System.out.println(Constants.SIZE);
        //System.out.println(Constants.initialDirtProb);
        //System.out.println(Constants.dirtyProb);
        //System.out.println(Constants.dirtSensorFailProb);
        //System.out.println(Constants.bumpSensorFailProb);
//        System.out.println("\nbump:" + bump);
//        System.out.println("dirty:" + dirty);
//        System.out.println("row:" + row);
//        System.out.println("col:" + col); 
        //int r = Constants.randomGen.getInt(4);


        Set<int[]> walls = new HashSet<int[]>();
//        int[] wallRoom = new int[2];

        if (dirty)
        {
            action="suck dirt";
        }
        else
        {
            if(bump == true){
                this.addWall(walls, row, col, action);

                System.out.print("Walls:" + walls);
                Iterator value = walls.iterator();
                while(value.hasNext()){
                    System.out.print("," + (int[])value.next());
                }
                System.out.println();
            }

            Random r = new Random();
            int i = r.nextInt(4);
            while(this.wallCheck(walls, row, col, i)){
                System.out.println("wallcheck: " + this.wallCheck(walls, row, col, i));
                i = r.nextInt(4);
            }


            if(i == 1) {
                action = "up";
            }
            else if(i == 2) {
                action = "right";
            }
            else if(i == 3) {
                action = "down";
            }
            else {
                action = "left";
            }

        }
     return action;
    }
}

I am actually having 2 problems:1) The method wallCheck isn't running and I'm assuming it's because the int[] values are being saved as references and not values. How can I check if my HashSet contains a specific int[] value? (used to locate the walls on a map). 2) Why are my values being cut off when I print out my HashSet "walls". They are printing out like this:

Walls:[[I@1515e375],[I@1515e375
Walls:[[I@4abd1f59],[I@4abd1f59
Walls:[[I@e4612e6],[I@e4612e6
Walls:[[I@6936b791],[I@6936b791

Thanks in advance!

-------------------------------- EDIT -------------------------------

Ok guys I figured out what the problem was. Like Dave said it Java doesn't print out array's nicely, so I had to print it out myself. Also, I was declaring the HashSet INSIDE the action method, that's why only 1 value was being stored.

  • `int[] temp = {row, col};` is creating an array with just two values in it - the value of row and the value of col – Scary Wombat Jan 29 '19 at 04:44
  • `dir == "up"` - this is not how you compre strings in java – Scary Wombat Jan 29 '19 at 04:44
  • 1
    see also https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Scary Wombat Jan 29 '19 at 04:45
  • `System.out.print("," + (int[])value.next());` results in `[[I@1515e375],[I@1515e375`. Java does not automatically print out the elements of an array nicely for you. You'll need to loop over each element or use a utility method. – dave Jan 29 '19 at 04:46
  • I know it's creating an array with just 2 values in it. I'm using it in place of tuples. Also thank you for letting me know about the str.equals() method. I didn't realize that's how you compare strings in Java. I haven't used Java in a while so I'm a bit rusty – I_am_learning_now Jan 29 '19 at 04:47
  • @dave Why are the values being cut off though? – I_am_learning_now Jan 29 '19 at 04:47
  • That seems to be the way Java works on objects, you get something like `[I@1515e375` for an object reference. – dave Jan 29 '19 at 05:00
  • @dave It ended up being cut off because the end part was my print statement. So it actually wasn't cut off. – I_am_learning_now Jan 29 '19 at 05:13

0 Answers0