Results 1 to 4 of 4

Thread: Java Error Question

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Angry 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.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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.

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Java Error Question

    and this applies to all 4 output

  4. #4

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: Java Error Question

    I didn't know that returned a void. Thx.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width