0

I'm trying to store 5 different boolean answers (true or false) in 5 different array positions after each loop and then make a method to display the questions which were 'true'.

For example, a test run would go like this:

Question1: Content1 ~ (True or False?) False

Question2: Content2 ~ (True or False?) True

Question3: Content3 ~ (True or False?) False

(loop finished)


Question2: Content2

(exit)

And here is my code so far.

import javax.swing.*;

class booleanTest {

    public static void main(String [] params) {


            String[] data = {"Test1", "Test2", "Test3", "Test4", "Test5"};
            boolean[] user = new boolean[5];

            array_input(data, user);
            System.out.println(user); // to see if it works atm

            System.exit(0);
    }

    public static String array_input(String[] a, boolean[] b) {

        String x = "";

        for (int i=0; i<a.length; i++) {

            x = JOptionPane.showInputDialog("Data: " + a[i]);

            if(x.equals("yes")) {
                b[i] = true;
            }
            else {
                b[i] = false;
            }
        }

            return x;
    }

    //public static String array_print() {

    // print the boolean + question here

    //}
}

It doesn't work, I understand that the b[i] = true part must be wrong, I should do something else?

10
  • You want to print each question & answer, Or only questions where the answer is true? Commented Nov 23, 2014 at 1:07
  • @August I want to print each question where the answer is true only, in the method array_print() Commented Nov 23, 2014 at 1:08
  • Can you please explain "it doesn't work"? Commented Nov 23, 2014 at 1:09
  • 1
    You have to use println(Arrays.toString(user)) to get the array in readable form. Commented Nov 23, 2014 at 1:12
  • 1
    nothing. With the way you're using it to modify the boolean array (whether that's good practice or not), it can be a void method. Commented Nov 23, 2014 at 1:37

3 Answers 3

2

If the value at an index of the boolean array is true, print out the value in the String array at that index.

public static void printTrue(boolean[] answers, String[] questions) {
    // checking both indices to avoid ArrayIndexOutOfBoundsException
    for (int i = 0; i < answers.length &&  i < questions.length; i++) {
        // if the answer is true
        if (answers[i]) {
            System.out.println(questions[i] + ": " + true);
        }
    }
}

When you say System.out.println(user); prints something like [Z@3b9187c7, this is because the toString() implementation for Object returns class name + @ + hex hashCode().

The Arrays#toString method creates a more readable result:

[false, false, false, true, true]
Sign up to request clarification or add additional context in comments.

1 Comment

@Xylus Call printTrue(user, data) in main?
1

You only have to return values in a method when you have no other way of accessing the data. If you look at your code you see that you're not even using the returned value, and the last values for x will never be useful anyway. In that kind of case, you can make it a void method. Void methods are used when you want it to perform some kind of operation, but don't need it to return any values. Your code works because an array is an Object and the changes done to it can be seen even outside the method.

Here's more or less how I would implement it. Notice the variable names are a little more descriptive.

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        String[] questions = {"Test1", "Test2", "Test3", "Test4", "Test5"};

        boolean[] responses = getUserResponses(questions);
    }

    public static boolean[] getUserResponses(String[] questions) {
        boolean[] responses = new boolean[questions.length]; //use the length of the other array. Don't count on it always being 5

        for (int i = 0; i< questions.length; i++) {

            String x = JOptionPane.showInputDialog("Data: " + questions[i]);

            if(x.equals("yes")) {
                responses[i] = true;
            }
            else {
                responses[i] = false;
            }
        }

        return responses;
    }
}

In general, it's better not to modify parameter Objects and to instead return new ones. Sometimes it is much more useful or necessary to do it that way, but in your case it was not.

2 Comments

I'm trying to sort of use this again in a different way but it's not working. (main method) trueFalse(inputFalseOrTrue); if(trueFalse = true) { System.out.println("It's true!"); } else if(trueFalse = false) { System.out.println("It's false!"); } trueFalse method public static boolean trueFalse(String test) { boolean trueOrFalse; if(test.equals("yes")) { trueOrFalse = true; } else { trueOrFalse = false; } return trueOrFalse; I understand there's many things wrong here but hopefully you understand what I'm trying to do. If not, no worries.. I'll figure it out :)
It's kind of difficult to read that and I'm not sure what the issue you're having is nor what you mean when you say "it's not working." I'd suggest you ask another question, explaining your intent, the error (or bad output) you're getting, and show your code. You can give me the link to your new question here and I'll take a look.
0

It looks like you're on the correct path.

Array creation and element assignment: Your code for this looks fine. You create the array using the new operator, as you would any object, but must specify an array size, as you did. Element assignment is done using an index, either an integer literal or an integer variable (in the case of your code above). Array indices range from 0 to size-1. It looks like you also got that part right.

Printing the results: In your scenario, you only want to some of the results, where the array value is true. A simple loop with an if(user[i]) would do the trick.

for (int i = 0; i < user.length; i++) {  
  if (user[i]) {  
    System.out.println(data[i] + " = " + true);
  }
}  

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.