-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!");
    }

}

}

Joe
  • 11
  • 2

4 Answers4

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.

Community
  • 1
  • 1
serj
  • 508
  • 4
  • 20
  • 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 05 '16 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);
hajjdjo
  • 1
  • 1
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') { ... }
Jeremy Yang
  • 29
  • 1
  • 7
-1

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

 charFromKB = "p"

Write

charFromKB == "p"
Mauro F.
  • 287
  • 2
  • 11
  • 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 05 '16 at 16:10
  • String comparison should be done using the `equals` method in Java, not using reference equality. – Mage Xy Feb 05 '16 at 16:14
  • Well, I never used java, why is that? – Mauro F. Feb 05 '16 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 '16 at 13:12