-1

Is there a basic solution I am missing here? Side note, I don't particularly want a super advanced solution as I am in the first 8 weeks of a computer science course in college and I feel it is either something simple I am missing or something I haven't learned yet.

Please focus on what I am asking, I know the code can be cleaner for now I don't want tips on how to make it more efficient I know there is too much repetition.

import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {

    /*Initializing a string which will be 
     *used in the future to determine the 
     *operator the user wants to use*/
    String operator = "z"; //Cant be A,S,M or D or the program would skip my while loop

    //Creating number variables which will initilised later by user input//
    int numOne;
    int numTwo;

    //Creating a boolean variable asking the user if they would like
    //to run another calculation and a String to store Yes or No
    boolean anotherCalc = true;
    String runAgain = "p";

    Scanner userOperator = new Scanner(System.in); 
    Scanner userNumbers = new Scanner(System.in);
    Scanner userAgain = new Scanner(System.in);

    while (anotherCalc == true) { //Containing the whole calculator in this while loop
                             //so it only stops when the user says they don't want another calc
        while (!operator.equalsIgnoreCase("A") && !operator.equalsIgnoreCase("S") 
                && !operator.equalsIgnoreCase("M") && !operator.equalsIgnoreCase("D") 
                && operator.equalsIgnoreCase("z")){
            System.out.println("What kind of calculation would you like to make?");
            System.out.println("Press A for Addition\nPress S for Substraction");
            System.out.println("Press M for Multiplication\nPress D for Division");
            operator = userOperator.next(); 
            } 

        System.out.println("\nThank you, now what numbers would you like to use?");
        System.out.println("Enter number 1:");
        String numError;
        numError = userNumbers.next();
        numOne = Integer.parseInt(numError);

        System.out.println("Enter number 2:");
        String numError2;
        numError2 = userNumbers.next();
        numTwo = Integer.parseInt(numError2);

        int answer;

        //Starting outer if statement containing nested if statements for
        //all 4 operators 

            //Addition if statement with nested if statement that won't
            //break the code if someone trys to add outside the int value range
        if (operator.equalsIgnoreCase("A")){
                    if (numOne > 1073741824 || numTwo > 1073741824){
                       long answerlong = (long) numOne + (long) numTwo;
                       System.out.print("\nYour answer is " + answerlong);
                    } else {answer = numOne + numTwo;
                       System.out.println("\nYour answer is " + answer);}

            //Subtraction if statement with nested if statement that won't
            //break the code if someone trys to subtract from a number above the int value range 
        } else if (operator.equalsIgnoreCase("S")){
                    if (numOne > 2147483647 || numTwo > 2147483647){
                        long answerlong = (long) numOne - (long) numTwo;
                        System.out.print("\nYour answer is " + answerlong);
                    } else {
                        answer = numOne - numTwo;
                        System.out.println("\nYour answer is " + answer);
                    }


        } else if (operator.equalsIgnoreCase("M")){
                    if (numOne > 20000 || numTwo > 20000){
                       long answerlong = (long) numOne * (long) numTwo;
                       System.out.print("\nYour answer is " + answerlong);
                    } else {
                       answer = numOne * numTwo;
                       System.out.println("\nYour answer is " + answer); 
                    }


        } else if (operator.equalsIgnoreCase("D")){
                    if (numOne % numTwo == 0){                              //Nested if else statement so
                    answer = numOne / numTwo;                               //an answer with no decimal    
                    System.out.println("\nYour answer is " + answer);    //point is shown as a whole 
                                                                           //number and otherwise return decimals
                   } else {                                     
                   float answerdec = (float) numOne / (float) numTwo; 
                    System.out.printf("\nYour answer is " + "%.2f",answerdec);} 


        } else {
            System.out.println("\nError");
        }

        System.out.println("\nWould you like to run another calculation?");
        System.out.println("Press Y for Yes or N for No");
        runAgain = userAgain.next();

        if (runAgain.equalsIgnoreCase("Y")){
            anotherCalc = true;
            operator = "z";     //This needs to be added so it returns to ask the user what type of calculation
            System.out.println("\n\n"); //This is just for aesthetics, creating 2 lines before the program runs again. Not included on the first run of the program on purpose. 
        } else if (runAgain.equalsIgnoreCase("N")){
            anotherCalc = false;
        } else {
            anotherCalc = false;
            System.out.println("\n\nInput Error (Y or N not entered)\nPlease reload the program");
        }

    }    
        System.out.println("\n\nThank you for using the calculator");
        System.out.println("I hope you enjoyed it");
AWT
  • 3,657
  • 5
  • 32
  • 60

1 Answers1

1

parseInt() will throw NumberFormatException, so it should be caught... so for example:

    try{
        numOne = Integer.parseInt(numError);
    }catch(NumberFormatException nfe){
        System.out.println("Error:  value entered is not a number");
        System.out.println("Please try again");
        continue;
    }
jwitt98
  • 1,194
  • 1
  • 16
  • 30
  • Excellent thank you very much, that has worked. – iconic Oreo Nov 02 '17 at 20:49
  • Now i am having an issue i didn't have earlier where the code i wrote to recognize a long is not working: Thank you, now what numbers would you like to use? Enter number 1: 2222222222 Error: value entered is not a number Please try again Any ideas? – iconic Oreo Nov 02 '17 at 21:17
  • You exceeded the maximum value for an integer. If you want to use values that large, you will need to convert your int variables to long and change numOne = Integer.parseInt(numError) to numOne = Long.parseLong(numError); – jwitt98 Nov 03 '17 at 01:35
  • Great, again thank you. I was trying to convert the numbers after the parse. – iconic Oreo Nov 03 '17 at 05:43