2

I am a student and i have a little problem with validation inputs.

String name;
System.out.println("Enter name:");
name = keyboard.nextLine();
//If name contain other than alphabetical character print an error

double number;
System.out.println("Enter number:");
name = keyboard.nextDouble();
//If number contain other than number print an error

What i have tried is test to parse the string to double but i could not. I have no idea how to test if the double is only number.

Please give me a clue of what i should do.

3
  • 1
    use regex [A-Za-z] for name and \\d for numbers. but dont you think name can contains other than alphabetical character, whitespace for example? Commented May 28, 2014 at 8:10
  • @christopher Not a duplicate, since this asks about validating both numeric and non-numeric inputs. But clearly heavily related. Commented May 28, 2014 at 8:20
  • Incidentally, a down-vote from me. This topic has been discussed numerous times on this site and you should have been able to find an answer with some prior research. Commented May 28, 2014 at 8:27

5 Answers 5

9

You can use Regular expression to check if the input match your constraint as follow :

String name;
System.out.println("Enter name:");
name = keyboard.nextLine();
if (!name.matches("[a-zA-Z_]+")) {
    System.out.println("Invalid name");
}
String number;
System.out.println("Enter number:");
number = keyboard.nextLine();
if (!number.matches("[0-9]+")) {
    System.out.println("Invalid number");
}

Here is a good tutorial to learn regex .

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

1 Comment

Almost right - but you must read the number as a string if you want regex to work. Plus, you should really indent your example code correctly.
1

You can loop through each character of the String and check if it's not alphabetic using Character.isAlphabetic(char):

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name:");
String name = keyboard.nextLine();

for (char c : name.toCharArray()) {
    if (!Character.isAlphabetic(c)){
        System.out.println("INVALID");
        break;
    }
}

To only accept numbers, you can do something similar using the Character.isDigit(char) function, but note that you will have to read the input as a String not a double, or get the input as a double and the converting it to String using Double.toString(d).

1 Comment

char c = evt.getKeyChar(); if (!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE) || contactMobile.getText().length() == 10) { evt.consume(); }
0
double number = 0;
try {
    number = Double.parseDouble(name)
} catch (NumberFormatException ex) {
    System.out.println("Name is not a double.");
}

If number is not a double, you can catch a NumberFormatException.

Comments

0

It seems you are using scanner. If you are, you can use the Scanner class' hasNextDouble() to check if the input is a double before reading the double as shown below:

double number;
System.out.println("Enter number:");
if (keyboard.hasNextDouble()){
    name = keyboard.nextDouble();
}

Have a look at the Scanner class docs for more information.

Comments

0

There is also the "fancier" regex solution. Java Pattern Documentation

You can use this (untested code) given you used nextLine() for reading BOTH inputs:

 boolean isWordOnly = Pattern.matches("\w*", name);    //name is in your code
 boolean isFloatOnly = Pattern.matches("\d*.?\d*", number);    //number is in your code too

Now the too boolean values tell you if there is the desired format in your input. You can add it in a do - while loop or anything you want.

It is a good idea to start studying reg(ular) ex(presions) because they are useful in string formatting (Imagine that you have to test if the input is a valid email...). Also used to check for SQL injections and many key things in apps and programs generally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.