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.

user3682556
  • 41
  • 1
  • 1
  • 2
  • 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? – Baby May 28 '14 at 08:10
  • @christopher Not a duplicate, since this asks about validating both numeric and non-numeric inputs. But clearly heavily related. – Duncan Jones May 28 '14 at 08: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. – Duncan Jones May 28 '14 at 08:27

5 Answers5

8

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 .

Mifmif
  • 3,132
  • 18
  • 23
  • 2
    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. – Duncan Jones May 28 '14 at 08:19
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).

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • char c = evt.getKeyChar(); if (!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE) || contactMobile.getText().length() == 10) { evt.consume(); } – gsm Jan 04 '20 at 04:29
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.

lpratlong
  • 1,421
  • 9
  • 17
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.

anirudh
  • 4,116
  • 2
  • 20
  • 35
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.

user2007447
  • 133
  • 1
  • 9