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

Fix :hover issues on iOS devices, universally with jQuery

javascript jquery ios hover touch

At the moment I'm having a following issue, when I first tap on something the :hover state gets triggered and I than need to tap second time in order to actually click an element. Fixes I found were

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.