0

I have a prompt to "Write a program that performs the following operations: +, -, *, /, % (as defined by Java). Your program should first read the first number, then the operation, and then the second number. Both numbers are integers. If, for the operation, the user entered one of the symbols shown above, your program should perform the corresponding operation and compute the result. After that it should print the operation and the result. If the operation is not recognized, the program should display a message about it. You do not need to do any input validation for the integers."

An example output I'm given is:

Enter the first number: 6
Enter the operation: +
Enter the second number: 10
6 + 10  =  16

How can I get started on this? I'm super confused and really new to java! Any help is greatly appreciated.

  • Think about using the Scanner class. Plenty of examples here on SO – Scary Wombat Feb 16 '16 at 00:29
  • 8
    What aspect of the problem do you have trouble with? Put differently, what have you tried? – meriton Feb 16 '16 at 00:31
  • 3
    Just as usual: split the problem up into smaller problems. E.g.: reading the values and the operations, and performing the operation itself. Just as a starting-point. Proceed with making smaller problems until you're at the point where you can actually write code that solves the problem. –  Feb 16 '16 at 00:32

5 Answers5

1

You generally first want to start reading input from STDIN:

Scanner in = new Scanner(System.in);

Then, I would read all parameters and afterwards perform the computation:

System.out.print("Enter the first number: ");
int left = Integer.parseInt(in.nextLine());

System.out.print("Enter the operation: ");
String operation = in.nextLine();

System.out.print("Enter the second number: ");
int right = Integer.parseInt(in.nextLine());

Now that all input is collected, you can start acting.

int result;
switch(operation)
{
    case "+": result = left + right; break;
    case "-": result = left - right; break;
    case "*": result = left * right; break;
    case "/": result = left / right; break;
    case "%": result = left % right; break;
    default: throw new IllegalArgumentException("unsupported operation " + operation);
}
System.out.println(left + " " + operation + " " + right + "  =  " + result);
randers
  • 5,031
  • 5
  • 37
  • 64
  • I haven't learned about the compute line or anything, this may be an easier way but is there a more basic method? – LolisMjolnir Feb 16 '16 at 00:45
  • @LolisMjolnir The `compute(int left, String operation, int right)` method defined in this code snippet is a custom method that the author had created to answer this specific question/purpose. It is not from a default library/API. Therefore, you would NOT have learnt about it in class. It is a user-defined method that makes use of a `switch` case tobdi the necessary if-else checks. You can read up on `if-else`, `switch case`, `while` and `do-while`. These are some of the basics that you should have learnt in class. – Tacocat Feb 16 '16 at 00:56
  • Please take note that syntax, API and such are language dependent. As such, do include `Java` as a keyword in your Googling efforts. Most of the answers here will recommend that you use the `Scanner` class, which you can and should read up on (since the basic reading and writing of input and output is really important): http://www.tutorialspoint.com/java/util/java_util_scanner.htm – Tacocat Feb 16 '16 at 01:02
0

To read the integers, use a Scanner

public static void main(String [] args)
{
  Scanner stdin = new Scanner(System.in);
  System.out.println("Enter the first number: ");
  int firstNum = stdin.nextInt(); //first number

  System.out.println("Enter the operation: ");
  String operation = stdin.next(); //operation

  System.out.println("Enter the second number: ");
  int secondNum = stdin.nextInt(); //second number

  doOperation(firstNum, secondNum, operation);
}

public static void doOperation(int firstNum, int secondNum, String operation)
{
  if(operation.equals("+")
  {
    int result = firstNum + secondNum;
  }
  else if(...)
  {
  //etc

  }
  System.out.println(firstNum + " " + operation + " " + secondNum + " = " + result);
}
Orin
  • 920
  • 5
  • 16
  • I'm unfamiliar with doOperation, I know I am supposed to be using if-statements but how can I just calculate it? – LolisMjolnir Feb 16 '16 at 00:51
  • doOperation is a defined method listed below the main. This is just to shorten code, but it is not needed. You can do everything that I have in the doOperation method in the main method instead. – Orin Feb 16 '16 at 00:53
0

Sounds like we are doing your homework! :) Make sure you learn these things or else it will eventually bite you. You can only delay the inevitable. With that "fatherly advice", here ya go.

First, you need to be able to read input from the console so that you can get the input numbers and operation. Of course, there are whole answers on this already. One link: Java: How to get input from System.console()

Once you have the input, then you can work with it.

You will need to look at the items entered. They say you don't need to validate the numbers but you need to validate the operation. So look at the operation String variable after you got it from the console and see if it is "equalsIgnoreCase" (or just equals since these symbols don't have uppercase) to each one of the accepted operations. If it isn't equal to any of them then you should print out a message as it says. (Again with System.out.println).

You can then go into some if conditions and actually do the math if the operation equals one of the items. For example:

if(inputOperation.equalsIgnoreCase("+")){
  double solution = inputInt1 + inputInt2;
//Need to do for all other operations.  I didn't do the WHOLE thing for you.
}else if(NEED_TO_FILL_IN_THIS){
//Need to fill in the operation.
//You will need to have more else if conditions below for every operation
}else{
System.out.println("Your operation of '"+inputOperation+"' did not match any accepted inputs.  Accepted input operations are '+','-','%','/' and '*'.  Please try again.");
}
System.out.println("Your answer to the equation '"+inputInt1+" "+inputOperation+" "+inputInt2+"' is the following:"+solution);

That should get you started. Let me know if you still need further direction.

I hope that helps!

And to end with some fatherly advice: Again, it sounds like you are doing homework. This is all pretty well documented if you just know how to google. "Java get input from console". Or "Java check if String is equal to another string". Learning how to fish is so much more important than getting the fish. I suggest you do some catchup because if this is your homework and you are unsure then it seems like you are a bit behind. I don't mean to be rude. I am just trying to help you longer term.

Community
  • 1
  • 1
BoBoCoding
  • 161
  • 1
  • 4
  • Thanks for the advice, I got super backlogged and I have 11 of these tasks due in 5 hours! I'm trying to get as much as help as I can since only now do I have time to submit it, and then learn what it was supposed to do later. – LolisMjolnir Feb 16 '16 at 01:08
  • @LolisMjolnir -- If the answer was correct or helpful, please up-vote it. That is how things work in stack overflow. – BoBoCoding Mar 28 '22 at 23:46
0

Enter the first number: 6 Enter the operation: + Enter the second number: 10 6 + 10 = 16

Scanner f=new Scanner(System.in)
System.out.print("Enter the first number: ")
int firstNum=f.nextInt();
System.out.println();

System.out.print("Enter the operation: ")
String Op=f.nextLine();
System.out.println();

System.out.print("Enter the Second number: ")
int secNum=f.nextInt();
System.out.println();

int answ=0;
if(Op.equals("+"){
   answ=firstNum+secNum;
}else if(.......){

}

hope it helps :)

Java jansen
  • 187
  • 2
  • 15
-2

Here is My Solution

package com.company;

import com.sun.org.apache.regexp.internal.RE;

import java.util.Scanner;

public class Main {
    private static Scanner scanner=new Scanner(System.in);

    public static void main(String[] args) {
    // write your code here

        int First,Second,Resualt;
        char operation;


        System.out.println("Enter the first number: ");
        First=scanner.nextInt();
        System.out.println("Enter the operation:");
        operation=scanner.next().charAt(0);
        System.out.println("Enter the second number :");
        Second=scanner.nextInt();


        if (operation=='+'){

            Resualt=First+Second;
            System.out.println(First+" "+"+ "+Second+" = "+Resualt);


        }

      else   if (operation=='-'){

            Resualt=First-Second;
            System.out.println(First+" "+"- "+Second+" = "+Resualt);


        }

        else if (operation=='*'){

            Resualt=First*Second;
            System.out.println(First+" "+"* "+Second+" = "+Resualt);


        }
        else if (operation=='%'){

            Resualt=First%Second;
            System.out.println(First+" "+"% "+Second+" = "+Resualt);


        }
        else {
            System.out.println("Error");
        }

    }
}

Good Luck!!

Ali
  • 1
  • 4