2

I have an assignment where I need to create a 3x5 array and ask the user for a boolean input. I then need to print the user input into every cell of the array. I'm stuck on how to use a for loop to enter the user input into the array. I also have to do this using methods. My code so far is:

import java.util.*;

public class TrueFalse
{
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args)
    {
        boolean myA[][] = new boolean [5][3];
        popArray(myA);
    }

    public static void popArray(boolean answ, boolean pArray[][])
    {
        System.out.println("Enter true or false.");
        answ = console.nextBoolean();

        for (int i=0; i<pArray.length; i++)
        {
            pArray[i] = answ;
        }
    }
}
1
  • Are you getting any errors? If so, what are they? Commented Feb 8, 2018 at 15:27

4 Answers 4

1

You're not far off:

for (int i=0; i<pArray.length; i++) {
    for (int j=0; i<pArray[i].length; j++) {
        System.out.println("Enter true or false.");
        pArray[i][j] = console.nextBoolean();
    }
}

will do the trick. Note you defined a matrix with 5 rows and 3 columns, the opposite of what you write in the text. Also note I'm checking nothing here.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is not far off. Try iterating over the bounds of the array in your popArray method:

public static void popArray(boolean pArray[][]) {
    for (int r=0; r < pArray.length; ++r) {
        for (int c=0; c < pArray[0].length; ++c) {
            System.out.println("Enter true or false.");
            boolean answ = console.nextBoolean();
            pArray[r][c] = answ;
        }
    }
}

One convenient option for printing your 2D array is Arrays.deepToString(), e.g.

System.out.println(Arrays.deepToString(pArray));

5 Comments

How would I go about printing the array?
@RyanAsciutto Use Arrays.deepToString(), at least this is one easy option.
Would I put that in the "main" method? What about calling the method "popArray"? That's something I have to do.
It's giving me the out put where it asks the user for true or false but when I enter one in it just keeps looping it and asking for it again and again. ""Enter true or false. false Enter true or false. true Enter true or false. true""
@RyanAsciutto I think we've given you enough help already. See if you can't figure out the rest on your own.
0

To make 3*5 array of boolean you should do

 boolean myA[][] = new boolean [3][5];

And make nested loop. and you can index the value to the array variable myA[ i ][ j ]

for(int i=0;i<3;i++){

 for(int j=0;j<5;j++){
    myA[i][j]=false;
  }

}

Comments

0

In your code you have a two dimensional array but your not assessing it correctly. You can visualized your 2d array as a matrix with 5 rows and 3 columns. So to assess every location you need to specify the row and column numbers. Your code should look like this:

private static int ROWS= 5;
private static int COLUMNS = 3;
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
    boolean myA[][] = new boolean [ROWS][COLUMNS];
    popArray(myA);
}

public static void popArray(boolean answ, boolean pArray[][])
{
    System.out.println("Enter true or false.");

    for (int i=0; i<COLUMNS ; i++)
    {
        for (int j=0; j<ROWS; j++)
        {
            pArray[i][j] = console.nextBoolean();
        }

    }
}

Comments

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.