0

I'm new to java and I'm wondering if there is a different method that I can use instead of has.nextInt() as this method messes up my scanner. For example

do {
    System.out.println("Please enter your full name: ");
    memberName = input.nextLine();
    if (input.hasNextInt()) {
        System.out.println("Your name cannot contain a number");
        input.next();
    } else {
        successful = true;
    }
} while (successful == false);

console

Create new member
Please enter your full name: 
jack 
jack

I have to enter my name twice before it moves on I know theres questions out there like this but I've had no luck with any of there solutions. Thanks

EDIT

I'm trying to make sure that the input does not contain any number at all and if it does then

System.out.print("Your name cannot contain any numbers");

happens

C.Darwin
  • 25
  • 1
  • 6
  • 2
    What do you expect `input.hasNextInt()` to do? – PM 77-1 Dec 08 '15 at 20:17
  • Can you provide some examples of names you want to accept? Like do you want to accept `foo123`? Or if someone has two names like `Jack Adam Smith`? – Pshemo Dec 08 '15 at 20:22
  • I'm only looking to accept letters only like joe blogs , jon stevens. I'm not looking to accept any number or foo123 @Pshemo – C.Darwin Dec 08 '15 at 20:38
  • Perhaps this SO post will answer your question: [Check if String contains only letters](http://stackoverflow.com/questions/5238491/check-if-string-contains-only-letters) – CubeJockey Dec 08 '15 at 21:00

5 Answers5

1

The usage of if(input.hasNextInt()){ is wrong here. As you want to find numbers in your previous entered string which is memberName

You could use regex for this purpose:

Pattern digitPattern = Pattern.compile("\\d+");       

And then you can use this to validate any string:

System.out.println(digitPattern.matcher("Marcinek 234").matches());
Marcinek
  • 2,144
  • 1
  • 19
  • 25
1
do {
    System.out.println("Please enter your full name: ");
    memberName = input.nextLine();
    if (memberName.contains("1234567890")) {
        System.out.println("Your name cannot contain a number");
    } else {
        successful = true;
    }
    input.next();
} while (successful == false);

You were using input in your if-statement to check, but you assigned memberName to the whole line.

liquidsystem
  • 634
  • 5
  • 15
1

Try creating your own method which will test if passed name is valid. It can look for instance like this:

private static boolean isValidName(String name){
    return name.matches("[a-z]+(\\s[a-z]+)*");//can be optimized with precompiled Pattern
}

Now your code can look like:

System.out.println("Please enter your full name: ");
do {
    memberName = input.nextLine();
    successful = isValidName(memberName);
    if (!successful) {
        System.out.println("Your name is not valid. Valid name can contain only letters and spaces. No digits are allowed.");
        System.out.println("Please try again: ");
    } 
} while (!successful);
System.out.println("welcome: "+memberName);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

If you want just read line from user input you can use:

hasNextLine()

Or you can just use:

hasNext()

And try to change this:

 while (successful == false);

With this:

  while (successful); 
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

You could use: enteredName.contains("1") to check for numbers. For example:

boolean containsNumbers = false;
for(int i = 0; i<9; i++){
if (name.contains(""+i)) containsNumbers = true; 
}
infinitecode
  • 33
  • 1
  • 1
  • 7