JohnH
JohnH

Reputation: 17

error: incompatible types: char cannot be converted to String

What am I doing wrong. My operator called operation is already a string.

javac Arithmetic2.java

Arithmetic2.java:44: error: incompatible types: char cannot be converted to String
                        case '+' : { result = add; }
                             ^
Arithmetic2.java:45: error: incompatible types: char cannot be converted to String
                        case '-' : { result = sub; }
                             ^
Arithmetic2.java:46: error: incompatible types: char cannot be converted to String
                        case '*' : { result = mul; }
                             ^
Arithmetic2.java:47: error: incompatible types: char cannot be converted to String
                        case '/' : { result = div; }
                             ^
Arithmetic2.java:48: error: incompatible types: char cannot be converted to String
                        case '%' : { result = per; }
                             ^
5 errors

Arithmetic2.java

// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % )     on two numbers
// Input:  Interactive.
// Output:  Result of arithmetic operation

import javax.swing.*;

public class Arithmetic2
{
public static void main(String args[])
{
    double numberOne, numberTwo;
    String numberOneString, numberTwoString;
    String operation;
    double result;

    numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
    numberOne = Double.parseDouble(numberOneString);
    numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
    numberTwo = Double.parseDouble(numberTwoString);
    operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");

    // Call performOperation method here
    performOperation(numberOne, numberTwo, operation, result);

    System.out.format("%.2f",numberOne);
    System.out.print(" " + operation + " ");
    System.out.format("%.2f", numberTwo);
    System.out.print(" = ");
    System.out.format("%.2f", result);

    System.exit(0);

} // End of main() method.

// Write performOperation method here.
public static double performOperation(double numberOne, double numberTwo, String operation, double result) {
    double add = numberOne+numberTwo;
    double sub = numberOne-numberTwo;
    double mul = numberOne*numberTwo;
    double div = numberOne/numberTwo;
    double per = numberOne%numberTwo;

    switch(operation) {
        case '+' : { result = add; }
        case '-' : { result = sub; }
        case '*' : { result = mul; }
        case '/' : { result = div; }
        case '%' : { result = per; }
        default : { System.out.println("Invalid operator!"); }
    }
}
} // End of Arithmetic2 class.

Upvotes: 1

Views: 1205

Answers (5)

Elliott Frisch
Elliott Frisch

Reputation: 201399

operation is a String, but your case statements all use char constants. Option 1: Change the switch, like

switch(operation.charAt(0))

Option 2: Change the case statements, like

switch(operation) {
    case "+" : { result = add; }
    case "-" : { result = sub; }
    case "*" : { result = mul; }
    case "/" : { result = div; }
    case "%" : { result = per; }
    default : { System.out.println("Invalid operator!"); }
}

However, in both cases the {} aren't meaningful and the case statements will fall-through - add breaks!

switch(operation) {
    case "+" : result = add; break;
    case "-" : result = sub; break;
    case "*" : result = mul; break;
    case "/" : result = div; break;
    case "%" : result = per; break;
    default : System.out.println("Invalid operator!"); break;
}

Upvotes: 1

Nikolas
Nikolas

Reputation: 44368

Since JOptionPane::showInputDialog in most cases returns String (except the one that requires lots of arguments to help it return a correct object type), it's better to use the String in the subsequent flow.

Simply change the switch cases quotation marks from '' (char) to "" (String). You don't need to change the method arguments.

switch(operation) {
    case "+" : { result = add; break; }
    case "-" : { result = sub; break; }
    case "*" : { result = mul; break; }
    case "/" : { result = div; break; }
    case "%" : { result = per; break; }
    default : { System.out.println("Invalid operator!"); break; }
}

Edit: Don't forget breaks.

Upvotes: 2

landru27
landru27

Reputation: 1702

Yes, operation is a string, but the error is telling you that '+' is not a string.

In Java, double quotes are for making a string literal, and single quotes are for making a char literal. You are trying to compare a string to a char.

If you change '+' (and the rest of your case operands) to "+", it should work.

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

Convert the operation variable to a char type and it would work.

Or change the switch options to String by replacing the single quotes with double as it will change the options to String

Upvotes: 0

user628985
user628985

Reputation:

You need to replace the single quotes with double quotes, as Java interprets single quotes as a char, so your code would need to read:

switch(operation) {
    case "+" : {result = add; }  // and so on...

}

Hope that helps a little :-)

Upvotes: 0

Related Questions