I want first to input the first number then the second number. After that input sub or add. If sub, 1st num-2nd num. If add, 1st num+second num. Then print the sum/difference. I started a code that will input 2 numbers then add. Please revise this one i can't revise it. Thank you!

import java.io.*;
public class reviewExer1 {
    public static int addNum(int xVar, int yVar){
        int result = 0;
        result = xVar + yVar;
        return result;
        public static void main (String[]args){
    int xVal = 0;
    int yVal = 0;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    String xinput = "0";
    String yinput = "0";
    System.out.print("Input x");
    try {
        xinput = dataIn.readLine();
    catch (IOException e) {
    xVal = Integer.parseInt(xinput);
    System.out.print("Input y:");
    try {
        yinput = dataIn.readLine();
    catch (IOException e) {
    yVal = Integer.parseInt(yinput);
    int resultOutput = 0;
    resultOutput = addNum(xVal,yVal);
    System.out.print("The sum is " + resultOutput);

Edited by John A: added code tags

Votes + Comments
After 34 posts you should be able to use code tags. There's no excuse for not using them.
7 Years
Discussion Span
Last Post by coud_ren_26

You need to take a third input, right? So start with that, then maybe you can figure out how to continue.


No. I am not going to do your homework for you. You already read lines from the user in that code and I can guarantee you've already used an if statement at somepoint in your course. You can at least make an attempt at doing your own hoemwork. Then again, I can pretty much guarantee that this isn't your code either, and so you are lost in the woods. Well, you've been given enough hints to at least be able to find the edges of those woods on your own, and you are going to have to do that before I give you anything more.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.