Hello sireesha
This was very challenging for me (no IDE used

), but I have a solution!
import java.util.Scanner;
import java.io.*;
public class Scanner_Tokenizing
{
public static void main(String[] args) {
try{
System.out.println("Enter some string:");
DataInputStream input = new DataInputStream(System.in);
String text = input.readLine();
Scanner scanner = new Scanner(text);
int count = 0;
while (scanner.hasNext() == true){
String st = scanner.next();
count++;
System.out.println(st);
}
System.out.println("Total number of tokens : " + count);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
}
The fragments in blue are the important ones. Remember that the System.in input stream can be infinitively long. So, the scanner does not know that a new line symbol means stop. That's why I read the input stream with a DataInputStream object before hand, to obtain a finite String object to work with. Also remember that when you compare stuff, you must use "
=="
I hope this helps!
