my program cant be resert to ask the user input data again after
while(ans.equalsIgnoreCase("y"); pls somebody can tell me!

Dani AI

Generated

— quick diagnosis and corrections (building on and )

Common reasons a loop stops re-prompting and how to fix them:

  • Both the value prompt and the "continue" prompt must be executed inside the repeating block so they run on every iteration. If either prompt is only before the loop, the loop will keep repeating the old value.
  • The variable used in the loop condition must be reassigned from the latest user reply each iteration. If it is never updated, the condition never changes. Use a case-insensitive comparison when testing a yes/no reply.
  • A stray semicolon immediately after a loop header turns the loop into an empty loop; the following block then executes only once after the loop finishes.
  • Do not call System.exit just to “restart” or reset the program; that force-terminates the JVM and hides logic errors. Let the loop exit naturally, or use break/return, or reinitialize variables inside the loop.
  • For numeric input from dialogs, first test for a cancelled dialog (null) and then parse inside a try/catch to handle invalid numbers without aborting the loop. Trim the input before comparing or parsing.

Debug tips: add a temporary log/print of the control variable each iteration or step through with a debugger to confirm the prompts run and the control variable changes. correctly advised moving the input inside the loop; was right to suggest parsing inside the loop but System.exit is unnecessary and should be avoided.

Recommended Answers

All 2 Replies

Then put the reading of the temperature inside the do-while.

you must put this inside the loop.

numTemperature = JOptionPane.showInputDialog("Enter temperature:");
number = Integer.parseInt(numTemperature);

then put System.exit();
after the while statement.

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.