2

Please help with this. I am very new to this, and I need this badly. I need to create a program where it lets you choose from addition or subtraction. This is my current program:

import javax.swing.JOptionPane;

public class RationalZ {

    public static void main(String args[]) {

        JOptionPane.showMessageDialog(null,
            "Enter the letter A for Addition and B Subtraction");

        String choice = JOptionPane.showInputDialog(
            "A=Addition B=Subtraction");

        if (choice == "a") {

            String strNum1 = JOptionPane.showInputDialog(
                "Enter a Number");
            String strNum2 = JOptionPane.showInputDialog(
                "Enter a second number");

            int aNum1 = Integer.parseInt(strNum1);
            int aNum2 = Integer.parseInt(strNum2);

            JOptionPane.showMessageDialog(null, aNum1 + aNum2);

            System.exit(0);

        } else {
            System.exit(0);
        }
    }
}

What's wrong? Even in the first step I can't get it.

Smi
  • 13,850
  • 9
  • 56
  • 64
Richard
  • 21
  • 2

1 Answers1

6

You might want to review the difference between the == operator and the equals() method.

Addendum: It's fairly easy to find good information on Java String comparison methods; it's a little harder to find a good explanation of why == with String is usually wrong.

Addendum:

Do I use another if statement?

You've got a good if-then statement; now you need to expand it to an if-then-else statement. Notice how you can add more if-then statements after the else.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • my problem is that im stuck and i dont know what to do anymore T.T – Richard Dec 04 '10 at 02:51
  • @Richard: I've added two links above that may help. Don't hesitate to ask additional questions. – trashgod Dec 04 '10 at 02:57
  • i placed this if (choice equals(a)) { but i get errors uhmm what would be the correct way? of using the equals()? – Richard Dec 04 '10 at 03:24
  • @Richard: You're almost there; `equals()` is a _method_, so you'd use the `choice.equals("a")` syntax. Notice how `"a".equals(choice)` is a nearly equivalent test. Now, what if the user types "A"? Do you see a method for that? – trashgod Dec 04 '10 at 03:34
  • omg thanks so much ^__^ it worked! im gonna continue on now ^_^ thanks alot^_^ ill be postin if i get into trouble again! thanks man ^_^ – Richard Dec 04 '10 at 03:38
  • uhm now im stuck again, i we got it to work... but now i dont know how to let the person to choose b for subtraction... do i use another if statement? and if yes, where? – Richard Dec 04 '10 at 03:40
  • `if(condition1){ //code for sum } else if (condition2) { //code for subtraction } else { //everything else }` – Eternal Noob Dec 04 '10 at 05:10
  • nvm i figured it already bro ^_^ thanks neway.. now all i need is to make my users choice pretty – Richard Dec 04 '10 at 05:43
  • any suggestions on how to make them choose a or b? or how do you make them so that they see a box and tick a or b then press ok? – Richard Dec 04 '10 at 05:44
  • @Richard: You can put any components you need in a panel as shown here http://stackoverflow.com/questions/3002830 – trashgod Dec 04 '10 at 16:49