Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class SpeedRead {
- public SpeedRead() {
- String equation;
- String operand;
- int firstNum;
- int secondNum;
- int answer;
- try {
- BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
- while ((equation = reader.readLine()) != null) {
- operand = getOperand(equation);
- firstNum = getNumber(equation, 0);
- secondNum = getNumber(equation, 1);
- answer = getNumber(equation, 2);
- switch (operand) {
- case "+":
- System.out.println(firstNum + secondNum == answer? "Correct" : "Wrong");
- break;
- case "-":
- System.out.println(firstNum - secondNum == answer? "Correct" : "Wrong");
- break;
- case "*":
- System.out.println(firstNum * secondNum == answer? "Correct" : "Wrong");
- break;
- case "/":
- System.out.println(firstNum / secondNum == answer? "Correct" : "Wrong");
- break;
- }
- }
- } catch (IOException ignored) {}
- }
- private String getOperand(String equation) {
- for (String comp : equation.split(" ")) {
- if ((comp).matches("[0-9]+") ||
- (comp).matches("=")) continue;
- return comp;
- }
- return null;
- }
- private int getNumber(String equation, int index) {
- int count = 0;
- for (String comp : equation.split(" ")) {
- if (comp.matches("[0-9]+")) {
- if (count == index) {
- return new Integer(comp);
- } else {
- count++;
- }
- }
- }
- return 0;
- }
- public static void main(String[] args) {
- new SpeedRead();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement