Scanner Class Skips Over Whitespace

Scanner class skips over whitespace

Use Scanner's hasNextLine() and nextLine() methods and you'll find your solution since this will allow you to capture empty or white-space lines.

next() does't allow white space and nextLine() skips sodaType all together

That's because scanners work by separating the input into a sequence of 'tokens' and 'delimiters'. Out of the box, 'one or more whitespace characters' is the delimiter, thus, an input of:

Root Beer
Hello World
5

consists of 5 tokens: [Root, Beer, Hello, World, 5]. What you want is that this forms 3 tokens: [Root Beer, Hello World, 5].

Easy enough: Tell the scanner you intend for newlines to be the delimiter, and not just any whitespace:

Scanner s = new Scanner(System.in);
s.useDelimiter("\r?\n");

That's a regular expression that will match newlines regardless of OS.

Mixing nextLine() with any other next method in scanner leads to pain and suffering, so, don't do that. Forget nextLine exists.

Scanner skipping input, possible white space?

The nextInt() method of Scanner leaves the new line character out, that is, it does not consume it. This newline character is is consumed by your nextLine() method, which is why you do not see it wait for your input.

To avoid this, call sc.nextLine() after age = sc.nextInt();, then leave the rest of code unchanged.

        ...
System.out.print("Age: ");
age = sc.nextInt();
sc.nextLine(); //added

if(Gender.equals("F") && age >= 20)
{
System.out.print("\nAre you married, " + fName + " (Y or N)? ");
M = sc.nextLine();
M = M.toUpperCase();

if(M.equals("Y"))
{
type = "Mrs. ";
type = type.concat(lName);
}
...

Reading from scanner and skipping whitespace

trim the line before splitting it.

String[] buff2 =  reader.nextLine().trim().split("\\s+");

Scanner doesn't see after space

Change to String name = scanner.nextLine(); instead of String name = scanner.next();

See more on documentation here - next() and nextLine()

how can i ignore white space while scanning

From here:

"A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace".

So, your program splits the file according to whitespace first, and then .split(";"); splits these by ;.

You need to set the delimiter to ; as follows:

Scanner s = new Scanner(new File(testCase.getFileName())));
s.useDelimiter(";");
while (s.hasNext()) {
System.out.println(s.next());
}

How to read whitespace with scanner.next()

Scanner scanner = new Scanner(file).useDelimiter("'")

But this is highly inefficient. The better way forward would be: to read character by character:

private static void readCharacters(Reader reader)
throws IOException {
int r;
while ((r = reader.read()) != -1) {
char ch = (char) r;
doSomethingWithChar(ch);
}
}

Also see HERE

Scanner is skipping nextLine() after using next() or nextFoo()?

That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.

You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself).

Workaround:

  • Either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.nextFoo to consume rest of that line including newline

    int option = input.nextInt();
    input.nextLine(); // Consume newline left-over
    String str1 = input.nextLine();
  • Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. For example, you may convert to an integer using Integer.parseInt(String) method.

    int option = 0;
    try {
    option = Integer.parseInt(input.nextLine());
    } catch (NumberFormatException e) {
    e.printStackTrace();
    }
    String str1 = input.nextLine();


Related Topics



Leave a reply



Submit