Java help please

I am having trouble displaying to my console to make my results look like the ones below. These numbers are just examples the end result you should be able to use any numbers.

Operation     Result
25+5          30
25-5          20
25*5          125
25/5          5

Here is my code so far!

import javax.swing.JOptionPane;
public class Arithmetic {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //local variables
        String firstNumber;
        String secondNumber;
        int number1;
        int number2;
        int sum;

    // input = get First and Second Numbers from user
        firstNumber = JOptionPane.showInputDialog("Enter first Number");
        secondNumber = JOptionPane.showInputDialog("Enter Second Number");


    // convert numbers from string to integer
        number1 = Integer.parseInt(firstNumber);
        number2 = Integer.parseInt(secondNumber);

   // add the numbers
        sum = number1 + number2;
        sum = number1 - number2;
        sum = number1 * number2;
        sum = number1 / number2;

 //display the results

        System.out.println("Operation   Result" );
        System.out.println(number1 + "(+)" + number2 );


    }


}

Answers


First of all, you need to print out the results of each of the operations. Second of all, you are calculating results with

    sum = number1 + number2; 
    sum = number1 - number2; 
    sum = number1 * number2; 
    sum = number1 / number2; 

Each time you throw away the previous result and assign the new result to the variable sum. You probably want a different variable for each of these results.

You might also be asking about formatting the columns. First you should worry about outputting all the data. Once you get that much working then you can deal with formatting.


First you need to use separate variables for each operations. Like this,

int sum, subtract, multiply, divide;

Then you need to do calculations for each operations and store it in respective variables,

As code follows:

try{    
        sum = number1 + number2;
        subtract = number1 - number2;
        multiply = number1 * number2;
        divide = number1 / number2;

 //display the results

        System.out.println("Operation\tResult" );
        System.out.println(number1 + "(+)" + number2+"\t\t"+sum );
        System.out.println(number1 + "(-)" + number2+"\t\t"+subtract );
        System.out.println(number1 + "(*)" + number2+"\t\t"+multiply );
        System.out.println(number1 + "(/)" + number2+"\t\t"+divide );
    }
    catch(Exception e){
        e.printStackTrace();
    }

Here's a fixed/simplified version:

import javax.swing.JOptionPane;
public class Arithmetic {
    public static void main(String[] args) {
        int number1 = Integer.parseInt(
                JOptionPane.showInputDialog("Enter First Number"));
        int number2 = Integer.parseInt(
                JOptionPane.showInputDialog("Enter Second Number"));

        System.out.print("Operation \tResult\n" + 
                number1 + "+" + number2 + " \t\t" + (number1+number2) + "\n" +
                number1 + "-" + number2 + " \t\t" + (number1-number2) + "\n" +
                number1 + "*" + number2 + " \t\t" + (number1*number2) + "\n" +
                number1 + "/" + number2 + " \t\t" + (number1/number2) + "\n"
                );
    }
}

Need Your Help

is there any .net or delphi component for showing Catia files?

c# .net delphi components catia

is there any .net or delphi component for showing Catia file?

Does Silverlight have a performance advantage over JavaScript?

javascript silverlight performance

At a recent discussion on Silverlight the advantage of speed was brought up. The argument for Silverlight was that it performed better in the browser than Javascript because it is compiled (and man...