-2

I'm really new to programming and I've been searching for days for a solution to this lab I'm working on. The lab is pretty simple and I believe I have the logic down, however when executing my code, I'm not getting the desired results. The program asks for input of three integers and one character. If the character is an 'S' the program will print the sum of the first 3 integers, if the character is a 'P,' the product, 'A,' the average, and any other character prints an error.

Below is my code. It asks for three integers and a character but the result is always an Error, even if i type in an 'S,' 'P,' or 'A.'

Any help would be greatly appreciated.

Thanks, Joe

    int n1, n2, n3;
    String numberFromKB;
    String charFromKB;
    Scanner keyboard = new Scanner (System.in);

    numberFromKB = JOptionPane.showInputDialog("Enter the first number.");
    n1 = Integer.parseInt(numberFromKB);

    numberFromKB = JOptionPane.showInputDialog("Enter the second number.");
    n2 = Integer.parseInt(numberFromKB);

    numberFromKB = JOptionPane.showInputDialog("Enter the 3rd number.");
    n3 = Integer.parseInt(numberFromKB);

    charFromKB = JOptionPane.showInputDialog(null, "Enter a character:");
    if (charFromKB = "s")
    {
        System.out.println("Sum of integers is: " + n1 + n2 + n3);
    }
    else if (charFromKB = "p")
    {
        System.out.println("Product of integers is: " + n1 * n2 * n3);
    }
    else if (charFromKB = "a")
    {
        System.out.println("Average of integers is: " + ((n1 + n2 + n3)/3f));
    }
    else 
    {
        System.out.println("ERROR!");
    }

}

}

3
  • 1
    You can not use = in an if clause. You probably want to use .equals() like this: charFromKB.equals("s")
    – user180100
    Feb 5, 2016 at 15:55
  • Does this even compile? Feb 5, 2016 at 15:56
  • see also the difference between = and ==
    – Rodney
    Sep 25, 2016 at 13:08

4 Answers 4

0

You're using the equal operator and thus assigning charFromKB to equal "S"

Instead you should use the string equals method, switch this:

if (charFromKB = "s")
{
        System.out.println("Sum of integers is: " + n1 + n2 + n3);
}

with this:

if (charFromKB.equals("s"))
{
       System.out.println("Sum of integers is: " + n1 + n2 + n3);
{

the equals operator "==" doesn't work well with Strings. More on this in the link.

1
  • I could have sworn I used .equals() before and it didn't work, but this time it did. Thank you so much Serj!!
    – Joe
    Feb 5, 2016 at 16:13
0

Use equals to compare two String. In your case : if (charFromKB.equals("s"))

    int n1, n2, n3;
    String numberFromKB;
    String charFromKB;

    String result = "";
    try {
        numberFromKB = JOptionPane.showInputDialog("Enter the first number.");
        n1 = Integer.parseInt(numberFromKB);

        numberFromKB = JOptionPane .showInputDialog("Enter the second number.");
        n2 = Integer.parseInt(numberFromKB);

        numberFromKB = JOptionPane.showInputDialog("Enter the 3rd number.");
        n3 = Integer.parseInt(numberFromKB);

        charFromKB = JOptionPane.showInputDialog(null, "Enter a character:");

        if (charFromKB.equalsIgnoreCase("s")) {
            result = "Sum of integers is : " + (n1 + n2 + n3);
        } else if (charFromKB.equalsIgnoreCase("p")) {
            result = "Product of integers is: " + (n1 * n2 * n3);
        } else if (charFromKB.equalsIgnoreCase("a")) {
            result = "Average of integers is: "+ ((n1 + n2 + n3) / 3);
        } else {
            result = "ERROR";
        }
    } catch (Exception e) {
        result = "ERROR";
    }

    JOptionPane.showMessageDialog(null, result);
0

First of all, your if statements are wrong:

= is used for assignment, so use == instead for comparison.

Also, double quote "" is used for present a String, so use single quote '' instead to present a char.

if (charFromKB == 's') { ... }

Second of all, lowercase 's,' 'p', and 'a' are different from uppercase 'S,' 'P', and 'A.

If you want to read those letters in uppercase only, you need to specify that.

if (charFromKB == 'A') { ... }

If you want the read those letters case insensitive, you have two options:

if (charFromKB == 'A' || charFromKB == 'a') { ... }

if (Character.toLowerCase(charFromKB) == 'a') { ... }
1
  • charFromKB is a string so you can't compare it to a char
    – Rodney
    Sep 25, 2016 at 13:09
-1

Try using the == operator instead of = in the if (), so instead of

 charFromKB = "p"

Write

charFromKB == "p"
4
  • Thanks for the reply! I originally used the == operand but run into the same issue. I mean I'm asking for a string and I'm saying if the string is S, A, or P, run the println. Unless I have that all wrong.
    – Joe
    Feb 5, 2016 at 16:10
  • String comparison should be done using the equals method in Java, not using reference equality.
    – Mage Xy
    Feb 5, 2016 at 16:14
  • Well, I never used java, why is that?
    – Mauro F.
    Feb 5, 2016 at 16:45
  • @MauroF because if two different string objects are compared using == that will return false, even if both objects contain the same text. This is why java object have an equals method - to compare different objects which are equal in content
    – Rodney
    Sep 25, 2016 at 13:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.