Develop a complete scanner You must show the execution of th
Develop a complete scanner. You must show the execution of this program by using a relevant source line as input, the program must show a list of the tokens in that line. (using Java)
Solution
solution
import java.util.Scanner;
import java.util.StringTokenizer;
public class TokensDemo {
public static void main(String[] args) {
// to initialize the scanner object
Scanner scanner = new Scanner(System.in);
System.out.println(\"enter the sentence \");
String sentence = scanner.nextLine();
//to split the sentence by using space
String[] tokens = sentence.split(\" \");
System.out.println(\"the tokens are \");
for (String token : tokens) {
System.out.println(token);
}
}
}
output
enter the sentence
rama killed ravana
the tokens are
rama
killed
ravana
