0

I have created this code (with the help of Reimeus). How would I achieve to instead of replace every value in the array with true to replace only one pre determined (by z for example) and this always being a value on the top column of the 2d array. Furthermore I would like to how I should start to get instead of the output being true and false it being x for true and y for false but still keeping the array an boolean

import java.util.*;

class Main {
    public static void main(String[] args) {
        int x;
        int y;
        Scanner scanner;
        scanner = new Scanner(System.in);
        x = scanner.nextInt();
        y = scanner.nextInt();
        boolean[][] cells = new boolean[x][y];

        for (int i = 0; i < cells.length; i++) {
            Arrays.fill(cells[i], true);
           System.out.println(Arrays.toString(cells[i]));
        }
    }
}
3
  • 3
    "it doesnt seem to work" is not best way to describe problem. What output you get and what you expect? Commented Sep 13, 2013 at 18:01
  • Sorry i am new here and still have a lot to learn thanks for the tip though Commented Sep 13, 2013 at 18:33
  • No problem. BTW welcome on StackOverflow :) Commented Sep 13, 2013 at 18:35

2 Answers 2

1

Use Arrays.toString to display the array content, otherwise the Object#toString representation of the Object arrays will be displayed

System.out.println(Arrays.toString(cells[i]));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks this works now i have a follow up question or two What would i need to do if i would want to make only 1 piece of the array true and decide where this true should go in the same way as i decide the lenght of the array. Seccondly is there a way in which i could replace the true statement with * and the false statement with -
@user2777431 This requirements should be part of your original question. Please edit your question and describe what you need and what do you have problem with.
I second @Pshemo's comment and please be very clear in what you're asking
I have just edited my file and i hope i am clear enough in what i am asking
@user2777431 as a rule on SO dont ask is it possible (where answer can be just yes/no) - instead ask "How do I..."
|
1

Aside from Reimeus answer:

What would I need to do if I would want to make only 1 piece of the array true and decide where this true should go in the same way as I decide the length of the array.

Using fact that boolean arrays are by default filled with false values you just need to read once index of element that should be set to true just like you did with lengths

boolean[][] cells = new boolean[x][y];

int trueX = scanner.nextInt();
int trueY = scanner.nextInt();
cells[trueX][trueY] = true;

also don't forget to remove Arrays.fill(cells[i], true);


Secondly is there a way in which I could replace the true statement with "*" and the false statement with "-"

You can create second loop that will iterate over elements of row and if it its value is true print * if not - like in this code:

for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        if (cells[i][j])
            System.out.print("* ");
        else
            System.out.print("- ");
    }
    System.out.println();
}

or just replace "true" string from result of Arrays.toString() with * and "false" with "-"

for (int i = 0; i < cells.length; i++) {
    System.out.println(Arrays.toString(cells[i]).replace("true", "*")
            .replace("false", "-"));
}

1 Comment

Yas! The replace() is exactly what I was looking for! Tnx mate! Also, you can for (boolean[] arr : grid) StdOut.println(Arrays.toString(arr).replace().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.