Kristian
Kristian

Reputation: 1239

Checking values in boolean array (Java)

I am having som slight difficulties with the following problem.

I have initialized a boolean array called numberArray with 31 indexes. The user is supposed to enter 5 digits between 1 and 30, and each time a digit is entered, the program is supposed to set the proper index to true. For instance, if I enter 5 then:

numberArray[5] = true;

However, if the user enters the value 5 a second time, a message should be given to the user that this number has already been entered, and so the user has to choose a different value. I have tried to create a loop as follows:

public void enterArrayValues() {
    for(int i = 1; i < 6; i++) {
        System.out.print("Give " + i + ". number: ");
        int enteredNumber = input.nextInt();
        while (numberArray[enteredNumber] = true) {
            System.out.println("This number has already been chosen.");
            System.out.print("Give " + i + ". number again: ");
            enteredNumber = input.nextInt();
        }
        numberArray[enteredNumber] = true;
    }
}

The problem is that when I run the program, I automatically get the message "The number has already been chosen" no matter what I enter. Even the first time I enter a number. I don't get this. Isn't all the values in the boolean array false by default?

I would greatly appreciate it if someone could help me with this!

Upvotes: 0

Views: 6455

Answers (5)

markdsievers
markdsievers

Reputation: 7319

while (numberArray[enteredNumber] = true) 

is an assignment, use the == operator or simply while (numberArray[enteredNumber]).

I know its hard to get into while you are still learning, but the earlier you start coding in an IDE the better off you will be. This is one tiny example of something an IDE will warn you about.

Upvotes: 2

millimoose
millimoose

Reputation: 39990

The condition in the while loop should be while (numberArray[enteredNumber] == true). You're using the assignment operator =, not the comparison operator ==. Assignment is an expression that returns the assigned value, which is true in your case.

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39102

Thde problem is in the condition of the while loop - you are using the assignment operator (=), whereas you are supposed to use the equality comparer (==). This way the loop condition is always true, because you are assigning true to the indexed field. I hope this will work :-) .

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48216

while (numberArray[enteredNumber] = true) {

make that

while (numberArray[enteredNumber] == true) {

or change to

while (true == numberArray[enteredNumber]) {

or simply drop the ==true

while (numberArray[enteredNumber]) {

Upvotes: 5

JRL
JRL

Reputation: 78033

Change the while line to:

while (numberArray[enteredNumber]) {

Because mistakenly entering = instead of == is a common mistake, some people always code this type of statement in the following manner:

while (true == numberArray[enteredNumber]) {

With this format, if you use = instead of ==, you will get a compiler error.

Also, if you use a type of static analysis tool such as PMD, I believe you get a warning for the statement that you originally wrote.

Upvotes: 1

Related Questions