-2

I have been struggling to figure out how to make my code loop when asking for user input.

Basically, I want the program to re-ask the question if the user enters no text at all. This is what I done so far.

import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

  public class Assessment {

    public static void main(String[] args) throws IOException {

          BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

          String me = JOptionPane.showInputDialog("Please enter your name");
          System.out.println("Your name is: " + me);

          String user1 = JOptionPane.showInputDialog("Please choose a number");
          System.out.println("Your number is: " + user1);

          String user2 = JOptionPane.showInputDialog("Please enter your choice of    security, choice1(low) or choice2(high)");
          String response = (String)System.in.toString();

          if(user2.equals("choice1"))
            JOptionPane.showMessageDialog(null,"your username is: "+me+user1,"Your username",JOptionPane.WARNING_MESSAGE);


    }
}
giampaolo
  • 6,906
  • 5
  • 45
  • 73

5 Answers5

1
while (!me.equals("")) {

}

to compare Strings in Java you have to use equals() and since you don't want it to be equal to empty text you should use the negation in Java.

Hope it helps.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
0

You could use a while loop that refers to what they entered: like while input = ""{question} or even a do while loop. See this question: Goto statements in Java

This may also help: http://www.tutorialspoint.com/java/java_loop_control.htm

Community
  • 1
  • 1
exitcode
  • 111
  • 4
  • 12
0

use java.util.Scanner.

Scanner input = new Scanner(System.in);

After prompting for the name, use the following:

String name = input_next();
if (name != null && !("").equals(name)) {  //then move next - else go back to the prompt
ali haider
  • 19,175
  • 17
  • 80
  • 149
0

This is all you need to do -

   String me = "";

          do
          {   
            me = JOptionPane.showInputDialog("Please enter your name");
          }while(me.equals(""));

Do it for your other windows too.

Or, just copy paste this code :(

import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assessment {

    public static void main(String[] args) throws IOException {

        BufferedReader userInput = new BufferedReader(new InputStreamReader(
                System.in));

        String me = "";
        String user1 = "";
        String user2 = "";

        do {
            me = JOptionPane.showInputDialog("Please enter your name");
        } while (me.equals(""));

        System.out.println("Your name is: " + me);

        do {
            user1 = JOptionPane.showInputDialog("Please choose a number");
        } while (user1.equals(""));
        System.out.println("Your number is: " + user1);

        do {
            user2 = JOptionPane
                    .showInputDialog("Please enter your choice of    security, choice1(low) or choice2(high)");

        } while (user2.equals(""));
        String response = (String) System.in.toString();

        if (user2.equals("choice1"))
            JOptionPane.showMessageDialog(null, "your username is: " + me
                    + user1, "Your username", JOptionPane.WARNING_MESSAGE);

    }
}
Trojan.ZBOT
  • 1,398
  • 2
  • 14
  • 23
0

You can find a great tutorial on this here, specifically the "while" and "do-while" statements.

In short :

while (condition) { 
   // statements
}

and

do {
   // statements
} while (condition);

As long as the condition evaluates to true, the statements will keep getting executed. The difference between while and do-while is the time at which the conditions are evaluated, refer to the tutorial for more information.

Shishigami
  • 441
  • 3
  • 7
  • 20