Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4.  
  5. public class SpeedRead {
  6.  
  7. public SpeedRead() {
  8. String equation;
  9. String operand;
  10. int firstNum;
  11. int secondNum;
  12. int answer;
  13.  
  14. try {
  15. BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
  16.  
  17. while ((equation = reader.readLine()) != null) {
  18.  
  19. operand = getOperand(equation);
  20. firstNum = getNumber(equation, 0);
  21. secondNum = getNumber(equation, 1);
  22. answer = getNumber(equation, 2);
  23.  
  24. switch (operand) {
  25. case "+":
  26. System.out.println(firstNum + secondNum == answer? "Correct" : "Wrong");
  27. break;
  28.  
  29. case "-":
  30. System.out.println(firstNum - secondNum == answer? "Correct" : "Wrong");
  31. break;
  32.  
  33. case "*":
  34. System.out.println(firstNum * secondNum == answer? "Correct" : "Wrong");
  35. break;
  36.  
  37. case "/":
  38. System.out.println(firstNum / secondNum == answer? "Correct" : "Wrong");
  39. break;
  40.  
  41. }
  42.  
  43. }
  44.  
  45.  
  46. } catch (IOException ignored) {}
  47. }
  48.  
  49. private String getOperand(String equation) {
  50.  
  51. for (String comp : equation.split(" ")) {
  52.  
  53. if ((comp).matches("[0-9]+") ||
  54. (comp).matches("=")) continue;
  55.  
  56. return comp;
  57.  
  58. }
  59. return null;
  60. }
  61.  
  62. private int getNumber(String equation, int index) {
  63. int count = 0;
  64.  
  65. for (String comp : equation.split(" ")) {
  66.  
  67. if (comp.matches("[0-9]+")) {
  68.  
  69. if (count == index) {
  70. return new Integer(comp);
  71. } else {
  72. count++;
  73. }
  74. }
  75. }
  76.  
  77. return 0;
  78. }
  79.  
  80.  
  81. public static void main(String[] args) {
  82. new SpeedRead();
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement