0

Ok, I have this code that asks an input for a username and a password. I used JOptionPane. What I want with the program is to display an error message if the the input on the username field has a number, and goes back to the previous dialog box asking for the username again. I have this while loop yet it does not function the way it should. Please do help. The program does not show the error message on my catch method and neither does it loop for the dialog box to display.

public class SwingExercise {

public static void main(String[] args) {

  String name = "";
  String pw = "";
  boolean input = true;
  boolean hasDigit = false;

  while (input) {
    try {
      while (name.equals("")) {
        name = JOptionPane.showInputDialog(null, "Enter username:");
        if (name.equals("")) {
          JOptionPane.showMessageDialog(
              null, "No input.", "Error", JOptionPane.ERROR_MESSAGE);
          name = "";
        }
        while (hasDigit) {
          for (int i = 0; i < name.length(); i++) {
            if (Character.isDigit(name.charAt(i))) {
              throw new InputMismatchException();
            }
          }
          hasDigit = false;
        }
      }
      input = false;

      while (pw.equals("")) {
        pw = JOptionPane.showInputDialog(null, "Enter password:");
        if (pw.equals("")) {
          JOptionPane.showMessageDialog(
              null, "No input.", "Error", JOptionPane.ERROR_MESSAGE);
          pw = "";
        }
      }
    } catch (NullPointerException e) {
      System.exit(0);
    } catch (InputMismatchException e) {
      JOptionPane.showMessageDialog(
          null, "Invalid input.", "Error", JOptionPane.INFORMATION_MESSAGE);
    }
  }
}

Please do mention any comments regarding the other lines in my code and if there are any unnecessary lines. Thank you in advance :)

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Layne
  • 25
  • 1
  • 5

3 Answers3

1

There seems to be absolutley no way to get into the while(hasDigit) loop because you have it set to false, and there is nothing setting it to true.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Lucas
  • 5,071
  • 1
  • 18
  • 17
  • 1
    I forgot what brought me back to this question, but I figured as long as I was here, I might as well leave the campground cleaner than I found it. :-) – Adam Liss Apr 09 '16 at 20:40
0

Some comments on style, since you asked:

  • In general, you want your try() blocks to be as short as possible, so it's clear which lines you're expecting to throw an exception. Otherwise you may unexpectedly catch an exception from a line that you didn't think could generate it, and then handle it incorrectly. This means the code will probably be longer, but it's far more important that it be easier to maintain and debug.

  • When it's legitimate for a string to be null, it's common to write if ("".equals(name)) rather than if (name.equals("")). Since the hard-coded empty string can never be null, you needn't surround the code with a try/catch block.

  • There's no need to set name or pw to the empty string inside an if() that tests to see if they're empty.

  • You probably don't want to echo the password. See JOptionPane to get password.

  • The code explicitly forbids digits in the name but accepts every other character, including punctuation and special characters. It's generally good programming practice to use a whitelist of acceptable input, rather than a blacklist of forbidden input. That way you'll never be surprised by invalid data that's unexpectedly accepted.

  • The outermost while() loop will exit if the password is invalid, since input only depends on the name. Better to set input to false at the top of the loop, then set it to true if either input is invalid.

  • Since the name and pw blocks are almost identical (they prompt for a non-empty string), consider extracting them into a helper function. You'll want to pass parameters that specify the prompt and whether or not to echo the input back to the user.

Community
  • 1
  • 1
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
0

One problem is that the value of hasDigit will always remain false. You probably will want to define hasDigit as true initially.

boolean hasDigit = true;
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44