I have a scanner, and have set the delimiter to "", but it still won't read whitespaces with the next() method. I know that nextline() works, but I need to examine every character in the input individually, including whitespace; it's for a complex data analysis problem. I'm stumped, though. Google has turned up nothing.
Can anyone help me with this? I'm thinking of reversing the whitespace into a special character, then for the purposes of analyzing the character, reverse it back into a space, contained in a string... That seems way overkill though! Is there a more elegant way of doing this?
EDIT: My main task is, take a String, and go over it character-by-character, multiple times, to examine the data for various tasks. You will have to examine it many times, one character at a time, and so I thought the scanner class would be my best bet since it can be easy to work with (for me at least). That's my task. Is there an easier way to go about this?
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
Try .useDelimiter("(\\b|\\B)")
It will use boundaries of every characters as delimiter.
The following code will print exactly what user typed.
No character will be ignored, including whitespaces.
Scanner charScanner = new Scanner( System.in ).useDelimiter( "(\\b|\\B)" ) ;
while( charScanner.hasNext() )
System.out.print( charScanner.next() ) ;
instead of
scanner.next();
use
scanner.nextLine();
This way you solve the problem with delimiters.
Use a BufferedReader
to read a line of input, then iterate the string in a for
loop, working with each char
.
You can check whether a character is a whitespace by Character.isWhitespace(char c)
.