Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this while terminate before receiving a value? (java)

Here's the relevant code snippet.

public static Territory[] assignTerri (Territory[] board, String[] colors) 
{ 
    for (int i = 0; i<board.length; i++) 
    { 
        // so a problem is that Territory.translate is void fix this. 
        System.out.print ("What team controls ") ; Territory.translate (i) ; System.out.println (" ?") ; 

        boolean a = false ;
        while (a = false) 
        {
            String s = getIns () ;
            if ((checkColor (s, colors)))
            {
                board[i].team = (returnIndex (s, colors)) ;
                a =true ; 
            }
            else 
                System.out.println ("error try again") ; 
        }       
        System.out.print ("How many unites are on ") ; Territory.translate (i) ; System.out.println (" ?") ; 
        int n = getInt () ; 
        board[i].population = n ; 
    }
    return board ; 

}

As an additional piece of information, checkColor just checks to make sure that its first argument, a string, is a string in one of the indexes of its second argument, an array.

It seems to me that when the while the method gets a string from the keyboard and then only if that string checks out is a true and the while allowed to terminate.

The output I get though is this:

What team controls Alaska ?
How many unites are on Alaska ?

(there is space at the end to type in an input)

This would seem to suggest that the while terminates before an input is ever typed in since the first line of text is within the while while the second line of text comes after it outside of it.

Why is this happening?

like image 995
David Avatar asked Feb 18 '26 06:02

David


1 Answers

Because you confused = with == ?

like image 79
mob Avatar answered Feb 19 '26 18:02

mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!