0

I want to validate the user input before the program continued. For example:

A variable String name. The program displays 'Enter name' and if the user types in a number instead of a String, a message should pop up and also make the user assign a String to name.

This is what I have so far:

System.out.println("Enter name");
String name = input.nextLine();

I tried try/catch but that did not work. I tried using

  if(input.hasNextInt()){System.out.println("Type in a string!");}

but that carries on through the program and still assigns a number to 'name'. It does not give the user a second chance to assign a string to 'name'

Tarikh Chouhan
  • 395
  • 3
  • 4
  • 13
  • Maybe you should consider using [regular expressions](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) for your validation? For example: [`[a-zA-Z\w]+`](https://regex101.com/r/gP7pM9/1) – mezzodrinker Nov 20 '15 at 20:09
  • Check [this](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) link out. It should answer your questions – Jonah Nov 20 '15 at 20:14
  • @Tarikh Chouhan try my answer below – Abdelhak Nov 21 '15 at 23:02

3 Answers3

0

Here is something to get you started using regular expression. It only checks for a String with characters between A-Z and a-z. See what happens when you try and enter "FirstName LastName" and see if you can figure out how to fix it.

Scanner input = new Scanner(System.in);
String name;
while(true){
    System.out.println("Enter name");
    name = input.nextLine();
    if (name.matches("[A-Za-z]+")){
        break;
    }else{
        System.out.println("Please enter only letters");
    }
}
System.out.println("Name selected: "+name);

The plus sign at the end of the brackets checks to see if you have at least one character. So if you enter a blank name, it will go to the else.

gonzo
  • 2,024
  • 1
  • 12
  • 24
0

The number checking can be achived fairly simple with the String matches() method. This method tells whether or not the string matches the given regular expression.

String name = "Shrek";

if (name.matches(".*\\d+.*")) {
    // Do whatever you want to do if the name contains a number
}
Tomasito665
  • 1,048
  • 1
  • 12
  • 23
-1

Try this solution that validate the input with pop up, when user inputs something invalid.

public class Test {

static String name;

public static void main(String[] args) {
    final JFrame parent = new JFrame();

    parent.pack();
    parent.setVisible(false);
    name = JOptionPane.showInputDialog(parent,
            "Enter name", null);

    while (true) {
        if (name.matches("[A-Za-z]+")) {
            break;
        } else {
             name = JOptionPane.showInputDialog(parent,
                    "Please enter only letters", null);
            if (name.matches("[A-Za-z]+"))
                break;

        }
    }
    System.out.println("Name selected: " + name);
  }

}
Abdelhak
  • 8,161
  • 4
  • 19
  • 34
  • Why not have the popup from the very start? And not just when user inputs something invalid. – gonzo Nov 20 '15 at 20:45
  • Now the code validate the input, when user inputs something invalid. – Abdelhak Nov 20 '15 at 21:27
  • 1
    @Abdelhak There is an error in your code, because when you enter letters after numbers, your program will display the number in **name** variable instead of letters. Because each time you create an other **`String` Variable name in `else` block** – Mourad BENKDOUR Nov 23 '15 at 11:49