samedi 7 novembre 2015

Looping a calculator code

I have this calculator code that uses both the users postfix expression and also a file input depending on the users preference, i need to create a loop that, if any of the postfix expression is incorrect, (3 3 +, 3 x 3,34 43 43 etc.) it already gives the user a correct error message regarding the error, but i need it to start the loop again for both the file input and the keyboard input, starting from the beginning where the user types in k or f for which input they would like.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {
public static void main(String[] args) throws FileNotFoundException {
    Scanner Scanner = new Scanner(System.in);
    System.out.println("Press K to enter the input using the keyboard. Press F to read expressions from a file");
    String option = Scanner.nextLine();
    while (!(option.equalsIgnoreCase("K") || option.equalsIgnoreCase("F"))) {
        System.out.println("Invalid option - please choose again");
        option = Scanner.nextLine();
    }
    if (option.equalsIgnoreCase("K")) {

        Scanner Scanner2 = new Scanner(System.in);

        System.out.println("Please enter a postfix expression: ");

        String line = Scanner2.nextLine();

        String[] elements = line.split(" ");

        if (elements.length != 3) {
            System.out.println("Error: Invalid Expression: " + line);

        }

        else {
            try {
                // "try" is a special statement which allows us to deal with
                // "exceptions"

                double num1 = Double.parseDouble(elements[0]);
                double num2 = Double.parseDouble(elements[1]);

                double answer = 0;
                boolean OpperationWrong = false;
                String operation = elements[2];
                switch (operation) {

                case "+": {
                    answer = (num1 + num2);
                    System.out.println("Answer is: = " + answer);
                }
                    break;

                case "-": {
                    answer = (num1 - num2);
                    System.out.println("Answer is: = " + answer);

                }
                    break;

                case "/": {
                    answer = (num1 / num2);
                    System.out.println("Answer is: = " + answer);

                }
                    break;

                case "*": {
                    answer = (num1 * num2);
                    System.out.println("Answer is: = " + answer);
                }
                    break;

                default:
                    OpperationWrong = true;
                }
                String ans;
                if (OpperationWrong) {
                    System.out.println("Please use +,*,/,- for your operator");
                } else {
                    ans = String.valueOf(answer);
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid Key entered, please enter a number for first 2 elements");
            }
        }
    } else if (option.equalsIgnoreCase("F")) {

        try {
            Scanner Scanner3 = new Scanner(System.in);
            System.out.print("Enter the file name to read expressions from : ");
            File file = new File(Scanner3.nextLine());
            Scanner3 = new Scanner(file);

            while (!(file.exists())) {
                System.out.println("Error: That file does not exist, please re-enter: ");

            }

            String line;

            while (Scanner3.hasNext()) {
                line = Scanner3.nextLine(); // read the next line of text
                                            // from the file
                String[] elements = line.split(" ");
                if (elements.length != 3) {
                    System.out.println("Error: Invalid Expression: " + line);
                } else {
                    try {
                        // "try" is a statement which allows us to deal with
                        // "exceptions"

                        double num1 = Double.parseDouble(elements[0]);
                        double num2 = Double.parseDouble(elements[1]);

                        double answer = 0;
                        boolean OpperationWrong = false;
                        String operation = elements[2];
                        switch (operation) {

                        case "+": {
                            answer = (num1 + num2);
                            System.out.println("Answer is: = " + answer);
                        }
                            break;

                        case "-": {
                            answer = (num1 - num2);
                            System.out.println("Answer is: = " + answer);

                        }
                            break;

                        case "/": {
                            answer = (num1 / num2);
                            System.out.println("Answer is: = " + answer);

                        }
                            break;

                        case "*": {
                            answer = (num1 * num2);
                            System.out.println("Answer is: = " + answer);
                        }
                            break;

                        default:
                            OpperationWrong = true;
                        }
                        String ans;
                        if (OpperationWrong) {
                            ans = " Please use +,*,/,- for your operator";
                        } else {
                            ans = String.valueOf(answer);
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("Invalid expression please enter a number for first 2 elements");
                    }
                }
            }
        } catch (FileNotFoundException exception) {
            System.out.println("The file could not be found");
        }
    }
}
}

Aucun commentaire:

Enregistrer un commentaire