Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test {
- public static void main(String[] args) {
- String[] numbers = "01234567890".toCharArray();
- String[] chars = "!@#$%^&*()_+}{[]".toCharArray(); // Put here whatever symbols you like
- Scanner sc = new Scanner(System.in); // Create the scanner, this scanner takes input from stdin
- System.out.print("Input a string: "); // This one is not println, print writes the string without a newline
- String input = sc.nextLine(); // Obtain the input from the user
- for (String word : input.split(" ")) { // For each loop, simply, this one means: For each word in input
- System.out.print("Token name: ");
- switch (word) { // This is a switch, it is faster (In nanoseconds scale) in this case and less cumbersome
- case "if":
- System.out.print("if");
- break; // DON'T FORGET TO BREAK BEFORE STARTING A NEW CASE! OTHERWISE WHEN THIS CASE MATCHES THE EXECUTION WILL CONTINNUE
- case "then":
- System.out.print("then");
- break;
- default: // It's like using else in an if condition
- if (inArray(word, numbers))
- System.out.print("numbers");
- else if (inArray(word, symbols))
- System.out.print("relops");
- else
- System.out.print("id");
- }
- System.out.println(); // Move to the next line, i.e. newline, aka \n
- }
- }
- public static boolean inArray(String word, String[] array) {
- for (String element : array)
- if (element.equals(word))
- return true;
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement