Advertisement
ayami123

Still have Errors

Feb 9th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. class Test {
  2.     public static void main(String[] args) {
  3.         String[] numbers = "01234567890".toCharArray();
  4.         String[] chars = "!@#$%^&*()_+}{[]".toCharArray(); // Put here whatever symbols you like
  5.         Scanner sc = new Scanner(System.in);    // Create the scanner, this scanner takes input from stdin
  6.         System.out.print("Input a string: ");   // This one is not println, print writes the string without a newline
  7.         String input = sc.nextLine();           // Obtain the input from the user
  8.         for (String word : input.split(" ")) {  // For each loop, simply, this one means: For each word in input
  9.             System.out.print("Token name: ");
  10.             switch (word) {                     // This is a switch, it is faster (In nanoseconds scale) in this case and less cumbersome
  11.                 case "if":
  12.                     System.out.print("if");
  13.                     break;                      // DON'T FORGET TO BREAK BEFORE STARTING A NEW CASE! OTHERWISE WHEN THIS CASE MATCHES THE EXECUTION WILL CONTINNUE
  14.                 case "then":
  15.                     System.out.print("then");
  16.                     break;
  17.                 default:                        // It's like using else in an if condition
  18.                     if (inArray(word, numbers))
  19.                         System.out.print("numbers");
  20.                     else if (inArray(word, symbols))
  21.                         System.out.print("relops");
  22.                     else
  23.                         System.out.print("id");
  24.             }
  25.             System.out.println();               // Move to the next line, i.e. newline, aka \n
  26.         }
  27.     }
  28.  
  29.     public static boolean inArray(String word, String[] array) {
  30.         for (String element : array)
  31.             if (element.equals(word))
  32.                 return true;
  33.         return false
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement