fork download
  1. /*
  2. Author: Brianna Scott
  3. Date: March 25th, 2017
  4. Purpose: Write a small program in the NEW LANGUAGE you're learning that illustrates all FIVE of these concepts.
  5. Insert comments in the code that point out to me where each of these concepts is illustrated.
  6. */
  7.  
  8. import java.util.*;
  9. import java.lang.*;
  10. import java.io.*;
  11.  
  12. class Main
  13. {
  14. public static void cscScanner(String[] args) throws java.lang.Exception
  15. {
  16. //Declare The variables
  17. char parenL = '(';
  18. char parenR = ')';
  19. char multi = '*';
  20. char add = '+';
  21. char[] expr;
  22. expr = new char[100];
  23. int i = 0;
  24.  
  25. //ask for expression input
  26. System.out.println("Please enter an expression \n");
  27.  
  28.  
  29. //begin the giant for loop that searches for the tokens and lexemes
  30. for (i = 0; expr[i] != null; i++)
  31. {
  32. //If the input is greater than a but less than z
  33. //Look-ahead to the next character in the input stream. If it’s letter or digit, then you have a 3-character ID.
  34. if (expr[i] >= 'a' && expr[i] <= 'z')
  35. {
  36. System.out.println(" The lexeme is " + expr[i]);
  37. System.out.println(" and Token is <ID> that only prints lowercase only. \n");
  38. }
  39. //Left parenthesis
  40. else if (expr[i] == parenL)
  41. {
  42. System.out.println(" The lexeme is " + expr[i]);
  43. System.out.println(" and Token is parenL.\n");
  44. }
  45. //right parenthesis
  46. else if (expr[i] == parenR)
  47. {
  48. System.out.println(" The lexeme is " + expr[i]);
  49. System.out.println(" and Token is parenR.\n");
  50. }
  51. //add operator
  52. else if (expr[i] == add)
  53. {
  54. System.out.println(" The lexeme is " + expr[i]);
  55. System.out.println(" and Token is add.\n");
  56. }
  57. //multiplication operator
  58. else if (expr[i] == multi)
  59. {
  60. System.out.println(" The lexeme is " + expr[i]);
  61. System.out.println(" and Token is multi.\n");
  62. }
  63. //else if it is invalid, print that it is.
  64. else
  65. {
  66. if (expr[i] != 'a' && expr[i] != 'z')
  67. {
  68. System.out.println("ERROR.\n");
  69. System.out.println("The lexeme " + expr[i]);
  70. System.out.println(" is invalid.\n");
  71.  
  72. }
  73. //If the expression is between an uppercase A and Z
  74. if (expr[i] >= 'A' && expr[i] <= 'Z')
  75. {
  76. //check to see if it is an uppercase letter
  77. System.out.println("The program will now check to see if the expression is valid.\n");
  78. System.out.println(" If the expression contains a uppercase, it will print a 1 (True). Else, It will return a 0 (False).\n");
  79. System.out.println(Character.isUpperCase(expr[i]));
  80. System.out.println("\n\n");
  81.  
  82. if (Character.isUpperCase(expr[i]) == true)
  83. {
  84. System.out.println(" The lexeme is not valid because it is uppercase. The correct version will print in ASCII.\n");
  85. System.out.println("The correct version is: \n" + Character.toLowerCase(expr[i]));
  86. System.out.println(" The program will now break.\n");
  87. break;
  88. }
  89. else
  90. System.out.println("Error. Something is incorrect with this program.\n");
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:30: error: incomparable types: char and <null>
		for (i = 0; expr[i] != null; i++) 
		                    ^
1 error
stdout
Standard output is empty