-1

Need a way to finish my 'advanced' calculator. i've been brainstorming with loops but im confusing myself. any ideas on how to implement the operands to the numbers without just nesting a bunch of if statements and hard coding through all of that.

import java.util.*;

public class calculator {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("welcome to your personal calculator. is your problem simple or advanced(multiple calculations)\n"
            + "Type 1 for simple or 2 for advanced");
    int choice = scan.nextInt();
    if(choice == 1){
        System.out.println("Simple equation");

        System.out.println("Enter the first number");
        int firstnum = scan.nextInt();
        System.out.println("Enter the second number");
        int secondnum = scan.nextInt();
        System.out.println("are you adding, subtracting multiplying or dividing? ENTER +,*,-, or /");
        String operand = scan.next();
        if(operand.equals("+")){
            int solution = firstnum + secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if (operand.equals("*")){
            int solution = firstnum * secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if(operand.equals("-")){
            int solution = firstnum - secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if(operand.equals("/")){
            int solution = firstnum / secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else{
            System.out.println("Did not recognize operation type please restart program.");
            return;
        }

        }

    else if(choice == 2){
        System.out.println("Advanced equation");
        System.out.println("How many numbers are in your equation? max of 7\n"
                + "operands are chosen by typing +,-,*,or /");
        int numofnumbers = scan.nextInt();
        if(numofnumbers == 2){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter operand");
            String operand = scan.next();
            if(operand.equals("+")){
                int solution = firstnum + secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if (operand.equals("*")){
                int solution = firstnum * secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if(operand.equals("-")){
                int solution = firstnum - secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if(operand.equals("/")){
                int solution = firstnum / secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else{
                System.out.println("Did not recognize operation type please restart program.");
                return;
            }

        }
        else if(numofnumbers == 3){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter thir num");
            int thirdnum = scan.nextInt();
        }
        else if(numofnumbers == 4){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
        }
        else if(numofnumbers == 5){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
        }
        else if(numofnumbers == 6){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
            System.out.println("enter sixth num");
            int sixthnum = scan.nextInt();
        }
        else if(numofnumbers == 7){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
            System.out.println("enter sixth num");
            int sixthnum = scan.nextInt();
            System.out.println("enter seventh num");
            int seventhnum = scan.nextInt();
        }
    }
    else{
        System.out.println("you did not type one or two. please restart program.");
        return;
    }

} }

  • http://stackoverflow.com/questions/29224513/java-beginner-code-snippet-help-code-not-working this question of mine also needs another answer. i got one answer solved from it but it still isent working right so it would be nice if i could get some suggestions for that problem too – taintedpyro813 Mar 25 '15 at 03:26
  • Instead of limiting your program to very specific input, you should think about how you can take a typical mathematical expression as a string and parse/evaluate it. Take a look here http://stackoverflow.com/questions/13662001/java-string-to-math-equation – user1274820 Mar 25 '15 at 04:51

1 Answers1

4

I love this challenge because it is so central to what computers were made for in the first place, calculating equations! What helps me to think about first is how the computer itself generally performs these kinds of operations. As I understand it the computer does one operation at a time (you got that part right!) and then stores that result and essentially re-evaluates the remaining problem as if that operation had already been done.

So 4+3+2

Simply becomes

7+2

and so on

In order to do this kind of thing in java I think it is best to familiarize yourself with a couple concepts such as ArrayList and String.split.

You need ArrayList to hold the problem instead of the more traditional array because the length of the problem is going to change throughout the program as we can see above with the 4+3+2 example. So if you were to try to cycle through an array with a for loop but the array needs to keep getting shorter, this can all become very complicated.

ArrayList is an awesome object that provides us with an dynamic "form" of array that has all kinds of helpful methods.

You can declare an ArrayList like this.

ArrayList<*variable type*> *Name* = new ArrayList<*same variable type*>();

And then you can use a bunch of different methods such as .add to add an item to the end of the list (increasing the length by 1), .remove to remove an item (and its containing index decreasing the length by 1), and .get(idx) which replaces Array[idx].

Now you are going to want to store your operators in the same object as your operands because you want to maintain their order. In order to do this your best bet is to store the whole entry as a String, like a sentence, and then later you can split the whole thing up. This is where String.split comes in.

You can check out this awesome entry on its use:

https://stackoverflow.com/a/13525053/4710112

Using this:

String.split("(?<=[-+*/])|(?=[-+*/])");

You can return an array that contains each number next to its operator in sequence.

So 4+3+2

becomes

4, +, 3, +, 2

Now this is very important!!

You cannot put the array returned from a split directly into an arraylist! You must store it in a traditional array first, and then you can "feed" it into an ArrayList using a for loop and ArrayList.add().

Now I'm not going to solve this for your, as I'm sure you'll want to figure it out yourself. But feel free to hit me up!

Oh and last couple things. You are going to need .parseToDouble to convert the strings in the ArrayList back into doubles, and .toString to convert them back into strings and place them back into ArrayList.

Oh and you should learn to write and call methods, will make your life a lot easier in terms of organization.

For example in main instead of:

double result;
double a;
double b;
result = a+b

You could do:

result = add(a,b);

As long as your write the method beneath main:

public double add(double x, double y){

double tempResult;
tempResult = x + y;
return tempResult;
}

May seem like more typing at first but when you get used to writing methods like that you will find your overall code getting shorter and shorter

Community
  • 1
  • 1