My loop keeps looping...even after the condition is changed to false. Whether I enter a "y" or an "n" my loop still loops. I know it's never leaves the loop, because I never get the "do I get here" println.

This is my first time with booleans...so maybe I'm doing something wrong.. with them.

public static void enterStudentNumber (int [] numberArray, String [] nameArray, Keyboard kbd)
	{
		boolean correctName = true;
		char response;
		int number;

		while (correctName = true )
		{

			System.out.println("Please enter a Student Number");
			number=kbd.readInt();


			System.out.println("Are you refering to student: "+nameArray[number]+" Enter a 'y' or a 'n'.");
			response=kbd.readChar();
			if (response == 'n')
			{
				correctName = false;
				System.out.println("i changed to false, so you better not loop");
			}

		}
		System.out.println("do i get here");
	}  //endmethod

Hehe. A classic.

correctName = true != correctName == true

The first one assigns true to your variable, the second one checks whether its value is true or not.

Black Box

man o man, I just spent a good 2 hours on that. The joys of learning, thanks man.

Hi..

Issue is in while( ) you have used single "=", you should use "==" to make it work..

Thanks

Hemant

My loop keeps looping...even after the condition is changed to false. Whether I enter a "y" or an "n" my loop still loops. I know it's never leaves the loop, because I never get the "do I get here" println.

This is my first time with booleans...so maybe I'm doing something wrong.. with them.

public static void enterStudentNumber (int [] numberArray, String [] nameArray, Keyboard kbd)
	{
		boolean correctName = true;
		char response;
		int number;

		while (correctName = true )
		{

			System.out.println("Please enter a Student Number");
			number=kbd.readInt();


			System.out.println("Are you refering to student: "+nameArray[number]+" Enter a 'y' or a 'n'.");
			response=kbd.readChar();
			if (response == 'n')
			{
				correctName = false;
				System.out.println("i changed to false, so you better not loop");
			}

		}
		System.out.println("do i get here");
	}  //endmethod

Of course, if its boolean, you don't need the == either. Simply while(correctName), you do realise that the result of an == is true or false, which is the exact value of the variable being checked, right?

This article has been dead for over six months. Start a new discussion instead.