|
-
Nov 15th, 2005, 06:38 PM
#1
Thread Starter
Hyperactive Member
Java Error Question
I wrote a crappy little calculator application for practice. When I go to compile the code, I get this exception returned:
Incompatible types:
Found: void
required java.lang.String
And then it points to a random place in the line of code that it says has an error. Here is the code:
Code:
import javax.swing.*;
public class JavaCalculator
{
public static void main(String[] args)
{
//declare variables
String choice;
String inputOperation;
String inputNumber1;
String inputNumber2;
String finalOutput;
int number1;
int number2;
//get the operation from the user
inputOperation = JOptionPane.showInputDialog(null, "Enter the type of operation you would like to perform.\n\n" + "Enter 'add', 'sub', 'mul', or 'div'. The field is not case sensitive.", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
//check for operation type
//perform operations accordingly
if (inputOperation.equalsIgnoreCase("add"))
{
//addition block
//get the numbers from the user
inputNumber1 = JOptionPane.showInputDialog(null, "Enter the first number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
inputNumber2 = JOptionPane.showInputDialog(null, "Enter the second number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
//convert the numbers so that they are useable
number1 = Integer.parseInt(inputNumber1);
number2 = Integer.parseInt(inputNumber2);
//output the final result
finalOutput = JOptionPane.showMessageDialog(null, "Your equation is: " + number1 + " + " + number2 + "\n" + "Your answer is: " + (number1 + number2), "Java Calculator", JOptionPane.PLAIN_MESSAGE);
}
else if (inputOperation.equalsIgnoreCase("sub"))
{
//subtraction block
//get the numbers from the user
inputNumber1 = JOptionPane.showInputDialog(null, "Enter the first number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
inputNumber2 = JOptionPane.showInputDialog(null, "Enter the second number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
//convert the numbers so that they are useable
number1 = Integer.parseInt(inputNumber1);
number2 = Integer.parseInt(inputNumber2);
//output the final result
finalOutput = JOptionPane.showMessageDialog(null, "Your equation is " + number1 + " - " + number2 + "\n" + "Your answer is: " + (number1 - number2), "Java Calculator", JOptionPane.PLAIN_MESSAGE);
}
else if (inputOperation.equalsIgnoreCase("mul"))
{
//multiplication block
//get the numbers from the user
inputNumber1 = JOptionPane.showInputDialog(null, "Enter the first number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
inputNumber2 = JOptionPane.showInputDialog(null, "Enter the second number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
//convert the numbers so that they are useable
number1 = Integer.parseInt(inputNumber1);
number2 = Integer.parseInt(inputNumber2);
//output the final result
finalOutput = JOptionPane.showMessageDialog(null, "Your equation is " + number1 + " * " + number2 + "\n" + "Your answer is " + (number1 * number2), "Java Calculator", JOptionPane.PLAIN_MESSAGE);
}
else if (inputOperation.equalsIgnoreCase("div"))
{
//division block
//get the numbers from the user
inputNumber1 = JOptionPane.showInputDialog(null, "Enter the first number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
inputNumber2 = JOptionPane.showInputDialog(null, "Enter the second number: ", "Java Calculator", JOptionPane.PLAIN_MESSAGE);
//conver the numbers so that they are useable
number1 = Integer.parseInt(inputNumber1);
number2 = Integer.parseInt(inputNumber2);
//output the final result
finalOutput = JOptionPane.showMessageDialog(null, "Your equation is " + number1 + " / " + number2 + "\n" + "Your answer is " + (number1 / number2), "Java Calculator", JOptionPane.PLAIN_MESSAGE);
}
System.exit(0);
}
}
The exception is usually thrown around the message output for the answer. What is wrong here? I don't see anything.
-
Nov 16th, 2005, 12:40 AM
#2
Re: Java Error Question
Code:
finalOutput = JOptionPane.showMessageDialog(null, "Your equation is " + number1 + " / " + number2 + "\n" + "Your answer is " +(number1 / number2), "Java Calculator", JOptionPane.PLAIN_MESSAGE);
take out finalOutput since showMessageDIalog returns void.
Last edited by oceanebelle; Nov 16th, 2005 at 12:44 AM.
-
Nov 16th, 2005, 12:45 AM
#3
Re: Java Error Question
and this applies to all 4 output
-
Nov 16th, 2005, 03:45 PM
#4
Thread Starter
Hyperactive Member
Re: Java Error Question
I didn't know that returned a void. Thx.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|