Scanner Doesn't See After Space

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()

Java - Scanner doesn't read string with space (Solved)

String t = "Chuck Norris";

t.matches("[a-zA-Z ]+")

Does in fact return true. Check for your input that it actually is "Chuck Norris", and make sure the space is not some weird character. Also instead of space, you can use \s. I also recommend 101regex.com

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

Program doesn't print anything until Scanner#nextLine() received not a blank space nor enter

Java Scanner hasNext() vs. hasNextLine():


That is, hasNext() checks the input and returns true if it has another non-whitespace character.

Whitespace includes not only the space character, but also tab space (\t), line feed (\n), and even more characters.

Continuous whitespace characters are treated as a single delimiter.

System.in means Standard Input.

When you pass System.in to init the Scanner, it will read data from Standard Input.

And it will always waiting for the input unless your input is EOF(Ctrl + Z in Windows or Ctrl + D).

So the scanner will always waiting for a non-whitespace character from input.

Input

When you press Space Enter, it sends two whitespace character \n to the Standard Input, and the function scanner.hasNext() is still waiting for a non-whitespace character. And scanner.hasNext() doesn't return anything. That's why there is no output at this time.

Then you press c, it sends non-whitespace character c to the Standard Input.

Output

Now your Standard Input contains \n c, the third one is not a whitespace character.

Finally the function scanner.hasNext() returns true.

Then the scanner.nextLine() read a line till character \n: it will be (one character),

and program print 1.

Standard Input now becomes c, only one character,

which will cause scanner.hasNext() to return true again:

Scanner will read a line, which will be one character c,

and print c 1.

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.

can't properly read in characters using 'Scanner' and '.next().charAt(0)'

You must use nextLine() instead of next() to read spaces. See more details here : Scanner doesn't see after space. Use isSpaceChar for checking space with a variable. See more details here : Checking Character Properties. The corrected code is....

/* reads a char, a space, or a period from keyboard, returns user input,
counts number of spaces and total number of entries */
package ch03_36_exercise_01;

import java.util.Scanner;

public class Ch03_36_Exercise_01 {

public static void main(String args[]) throws java.io.IOException {

Scanner userInput = new Scanner(System.in);
char keystroke; // character that user enters
int ctr = 0, spaces = 0; // num of tries to stop run, num of spaces entered

do {
// ask for user input
System.out.print("Enter a character, or hit the space bar,"
+ " or enter a period to stop: ");
keystroke = userInput.nextLine().charAt(0);

if (Character.isSpaceChar(keystroke)) {
System.out.println("You entered a space");
spaces++; // increment space bar count
} else {
System.out.println("You entered a " + keystroke);
}

// increment keystroke count
ctr++;
} while (keystroke != '.');

System.out.print("It took " + ctr + " tries to stop");

if (spaces > 0) {
System.out.println(" and you hit the space bar " + spaces + " times\n");
}
}
}

Java Scanner Reading Leading Space

Try the line ch = keyboard.next().charAt(0); switch(ch)

import java.io.*;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner keyboard = new Scanner (System.in);
char ch; int m,n; String y;
do
{
System.out.println("Enter Option (a,b,c,d,e,f,q):");
ch = keyboard.next().charAt(0);

switch (ch)
{
case 'c':

System.out.println("Enter 2 Numbers");
System.out.println("First Number: ");
m = keyboard.nextInt();
System.out.println("Second Number: ");
n = keyboard.nextInt();
for(int i = 1; i <= n - m + 1; i++)
{
if (i % 5 == 0)
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.println(", ");
}
}else
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.print(", ");
}
}

}
System.out.println("");
break;
}
}while (ch != 'q');

}

}

Happy coding!

Why scanner can't read any (empty) space between words (in my specific case)?

You need to change

name = sc.next();

sc.next() will read only till the space nothing after that

name = sc.nextLine();

sc.nextLine() will read the whole line.

Note you need to be careful of skipping read Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods.



Related Topics



Leave a reply



Submit