Forums Register Login

Test for quit loop (a simple one!)

+Pie Number of slices to send: Send
I am trying to write code for a loop that will test for the word "quit." The code I have so far is posted, but I don't really even understand it because I'm just "fishing" for code anywhere I can find it. (I am an English major!) If someone can help me understand this, I would greatly appreciate it. I think I need a do...while loop. I think my code isn't working because I have declared a quit as a boolean data type and it is comparing it to a String that the user enters? Thanks!
+Pie Number of slices to send: Send
Your problem is that you keep declaring String Object everytime you want to use it. By the way, you don't need the boolean quit for the program to work.
import javax.swing.JOptionPane;
public class Quit
{/** Main method*/
public static void main(String[] args) // JVM call this method.
{
String s = ""; // local variable should be explicitly initialized

do {s = JOptionPane.showInputDialog(null,"Enter a string: ,\n To quit program, enter quit","Input Box", JOptionPane.QUESTION_MESSAGE);
}while ( !s.equals ("quit"));
System.out.println(s);
System.exit(0);}}
+Pie Number of slices to send: Send
Try creating more vaiables with good names, that will often help you see what is happening. Also, to avoid unpleasant surpises, give all your variables initial values right away when you create them.
Hopefully this will help you see how and why this code does what it does.
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2147 times.
Similar Threads
Palindromes
Continious user input
Evaluating Conditions for While loop
Case Statement Problem
How to compare String and int?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:47:53.