i'm write a program that asks for a password then if correct will go into a while loop, but for some reason whenever i execute the code and enter the right pass, nothing happens. heres what i've been working on:

/*
  This program will simulate a program that could
  be used by an SAT tutoring company.
 */

 public class sat_tutor
 {
 	public static void main (String [] args)
 	{
 		EasyReader console = new EasyReader();
 		final String password = "1";
 		String passwordattempt;
 		String buffer;
 		int selection;
 		System.out.print("Please enter the password to enter the system: ");
 		passwordattempt = console.readLine();
 		buffer = console.readLine();

		boolean menu1 = false;
 		boolean menu2 = false;
 		boolean menu3 = false;

 		while(passwordattempt.equals(password));
 		{
 			System.out.println("S A T   H E L P   B I L L I N G");
 			System.out.println("            M E N U");
 			System.out.println("");
 			System.out.println("[1] TUTOR'S INFORMATION");
 			System.out.println("");
 			System.out.println("[2] STUDENT INFORMATION");
 			System.out.println("");
 			System.out.println("[3] STUDENT RECEIPT");
 			System.out.println("");
 			System.out.println("[4] QUIT PROGRAM");
 			System.out.println("");
 			selection = console.readInt();

 			if(selection == 1)
 			{
 				String tutorname;
 				int code;
 				System.out.print("Please enter your name: ");
 				tutorname = console.readLine();
 				buffer = console.readLine();
 				System.out.print("Please enter your personal 4-digit code: ");
 				code = console.readInt();
 				if(((code / 1000) == 7) && ((code - 9) % 10) == 0)
 				{
 					System.out.println("Code has been validated");
 					menu1 = true;
 				}
 				else
 				{
 					System.out.println("Invalid code.");
 				}
 			}
 		}
 	}
 }

i've given the code to my friend and he says that it runs fine on his computer and we both use the same compiler/ide (jcreator). i'm thinking that its not a problem w/ my code but w/ my computer. anyone have any ideas?

Dani AI

Generated

Short answer: the stray semicolon after the while condition is the root cause. As pointed out, a trailing semicolon makes the loop execute an empty statement. Because the loop condition compares a fixed input value, if the condition is true it will busy-wait forever (program appears to “do nothing”); if the condition is false the empty loop is skipped and the following block runs once — which explains why behavior can look inconsistent between runs or different users.

Why that explains the friend-vs-you observation: if your friend entered a different password (or your EasyReader returned a different string), the while condition was false and the menu printed. When you entered the correct password the condition evaluated true and the program spun in the empty loop. The block that prints the menu is not controlled by the while at all because of the semicolon.

Practical fixes and checks:

  • Remove the semicolon so the block becomes the loop body (while (condition) { ... }).
  • Add a short debug print of the password variable (or step through with the IDE debugger) to confirm what console.readLine() actually returns. Trim whitespace or use equalsIgnoreCase if appropriate.
  • Replace or test EasyReader with java.util.Scanner or BufferedReader if input behavior seems odd — as suggested, verifying the input helper class is good practice.
  • Use IDE inspections or static analysis (SpotBugs/Checkstyle) to catch accidental empty statements and other common pitfalls.

Caution: avoid accidental empty loops (e.g., while(true);) — they consume CPU and make the program unresponsive.

Recommended Answers

All 3 Replies

First question is what is your 'EasyReader' class? Can you post the code for it? I suspect the return from "passwordattempt = console.readLine();" is corrupt, but since console is from the 'EasyReader' class I can't tell.

Your complier is skipping your while loop because you have a semicolon after the while loop, this tells the program that that line is ended and so it doesn't execute the statements inside.

Dooh! Darn semicolon! Missed that one......hehe

Great eyes shdwdrgn517!

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.