-1

I am using java scanner to read input from System.in. I need to read alpha-numeric line including space/tab but not the new-line and should not allow empty input as well.

For example :

a new name

or

a-new-name-1

Here is my scanner:

Scanner reader = new Scanner(System.in);

I tried these ways:

String name = reader.nextLine();

or

String name = reader.next("^[A-Za-z0-9- ]+$");

or

name = reader.next("/^[a-z\\d\\-_\\s]+$/i");

For last 2 cases with input "a test name 1" , I had error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.next(Scanner.java:1418)
............

And when used reader.nextLine(),it's skips waiting for next input.For example:

For this part of the code:

System.out.println("Do you want to update audience name?[y/n]");
opt = reader.next().trim();

if( opt.equalsIgnoreCase("y") )
{
    System.out.println("Enter audience name : ");
    name = reader.nextLine();
}

System.out.println("Do you want to update audience description?[y/n]");
opt = reader.next().trim();

if( opt.equalsIgnoreCase("y") )
{
    System.out.println("Enter audience description : ");
    description = reader.nextLine();
}

I am seeing this:

Do you want to update audience name?[y/n]
y
Enter audience name : 
Do you want to update audience description?[y/n]
y
Enter audience description : 
Do you want to update audience rule?[y/n]

May I get any help here?

VictorGram
  • 2,199
  • 4
  • 31
  • 59
  • reader.nextLine() auto breaks sentences by \n. why reader.nextLine() not working on your case? – CSK Jun 29 '16 at 18:01
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Jonny Henly Jun 29 '16 at 18:10
  • Why don't you set `opt` equal to `reader.nextLine().trim()`? Why do you call `reader.next()`, just grab the whole line. Your problem is that the line break character is not being consumed in the `System.in` buffer, so when you call `description = reader.nextLine()` - `description` is getting set to whatever is still in the `System.in` buffer. – Jonny Henly Jun 29 '16 at 18:19
  • @Jonny, reader.nextLine() works if I don't mix up next() and nextLine(). But I also need to make sure that not taking null/empty entry. That's why I was trying reader.next(regex) but was not able to make it right. – VictorGram Jun 29 '16 at 18:28
  • The `Scanner` is not responsible for verifying user input, once enter is pressed then that is what is sent to the `System.in` stream. It doesn't re-prompt the user, it throws an exception. It is your job to parse the string returned by the `Scanner` to see if it is valid input, if it isn't then re-prompt for input. A simple while loop is all you need. – Jonny Henly Jun 29 '16 at 18:32
  • 1
    Thanks @Jonny, I was looking for an elegant way but finally handled in your suggested way – VictorGram Jun 29 '16 at 20:34

2 Answers2

0

You can use this:

String name = reader.next("^[\\w\\p{Space}-]+$");

there is no regex delimiter / in Java.

If you're using Java 8 then you can use \h to match horizontal whitespace:

String name = reader.next("^[\\w\\h-]+$");

Full Code:

Scanner reader = new Scanner(System.in);
reader.useDelimiter("\\R");
while(!reader.hasNext("^[\\w\\h-]+$")) {
    System.out.println("Only enter letters, numbers, hyphen, underscore or whitespace");
    reader.next();
}
String name = reader.next();
System.out.println("valid Input: " + name);     
reader.close();

Working Code Demo

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Enter audience name : a new aud name 1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.next(Scanner.java:1418) – VictorGram Jun 29 '16 at 18:11
  • 1
    The regex shouldn't matter. OP isn't consuming the new line character in the `System.in` buffer before calling `Scanner#nextLine()`. – Jonny Henly Jun 29 '16 at 18:15
  • @Patty: Check my updated answer with full code. You need to use `reader.useDelimiter("\\R");` if using Java8 or `reader.useDelimiter("[\\r\\n]");` – anubhava Jun 29 '16 at 18:48
  • @ anubhava This can not handle null-input – VictorGram Jun 29 '16 at 20:36
  • I thought you didn't want to allow empty input,when you wrote **should not allow empty input as well** – anubhava Jun 29 '16 at 20:41
0

FOllow Jonny Henly's suggestion. Mixing up of reader.next and reader.nextLine was the problem.

VictorGram
  • 2,199
  • 4
  • 31
  • 59