0

I am a noob to java. i am trying to create a while loop that will prompt the user after each process; as to whether or not they wish to continue. my progress looks like this.

    String proString,answer;
    char t,f,x;
    boolean exitTime;
    exitTime = false;
    proString = " Would You Like To Continue? \n";
    while(!exitTime) {
        continue;
    }
    answer = JOptionPane.showInputDialog(null," Would You Like To Continue? T/F ",HEADING,JOptionPane.INFORMATION_MESSAGE);
    x = char.parseChar(answer);

I have tried the following as well to no avail...

    Prompt= JOptonPane.showInputDialog(null,proString,HEADING,JOptionPane.QUESTION_MESSAGE); 
    input = char.parseChar(Prompt);
    if(input == "y") {
        break;
    }
    else if(input == "n") {
    }
    input = char.parseChar(Prompt);
    if(input != "y"||"n") {
        JOptionPane.showMessageDialog(null,"Invalid Input", "Error", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    }

I guess my question comes down to should i use JOptionPane.showInputDialog(...) or JOptionPane.showConfirmDialog(...) or another method? please show me how you would do this.

6
  • it would be a lot easier to help you if the code in your question was formatted properly. Commented Feb 27, 2015 at 0:17
  • hence 'i am a noob.' how can i create a while loop with my variables? Commented Feb 27, 2015 at 0:22
  • 2
    Can you provide code that compiles? As is there are many parts of your code with incorrect syntax. Commented Feb 27, 2015 at 0:23
  • what data type is the variable input Commented Feb 27, 2015 at 0:29
  • i was using 'char input' because i was trying to get the question to return a char since i couldnt convert string to char...but this may be the wrong thinking... Commented Feb 27, 2015 at 0:32

1 Answer 1

2

What you need is to use a do-while loop:

//assume user does not want to continue.
boolean continue = false;
do
{
//process whatever you want

//ask the user
Prompt= JOptonPane.showInputDialog(null,proString,HEADING,JOptionPane.QUESTION_MESSAGE);
input = char.parseChar(Prompt);
//wait until you get a valid input
while(input != "y"||"n")
{
JOptionPane.showMessageDialog(null,"Invalid Input", "Error",JOptionPane.INFORMATION_MESSAGE);
Prompt= JOptonPane.showInputDialog(null,proString,HEADING,JOptionPane.QUESTION_MESSAGE); 
input = char.parseChar(Prompt);
}
//if user wants to continue, change your flag to true
if(input == "y") {
    continue = true;
}
//otherwise make it false
else if(input == "n") {
    continue = false;
}
while(continue); //if continue is true loop back

I haven't tested it but it should explain you the logic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.