Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Integer class not working?

I'm doing an integer class a program that uses dialog boxes to obtain two integer values (one dialog box for each value), display the sum and product of the values, and use another dialog box to see whether the user wants to process another pair of values. I have the code here

import javax.swing.JOptionPane;

public class Integer3

{

public static void main (String args[])

{

new Integer3 ();

}

public Integer3()

{

//Get the user's first integer

String userinput = "yes";

while (userinput.equals("yes"))

{

String input = JOptionPane.showInputDialog ("Please enter the first integer>> ");

String input2 = JOptionPane.showInputDialog ("Please enter the second integer>> ");

int number1 = Integer.parseInt (input); //Used to convert from string to integer

int number2 = Integer.parseInt (input2); //Used to convert from string to integer

System.out.println ("The sum of the two numbers is: " + (number1 + number2));

System.out.println ("The product of the two numbers is: " + (number1 * number2));

System.out.println ();

String userinput = JOptionPane.showInputDialog ("Do you want to continue? ");

}

}

}

But then it's not working. I get error messages like this:

Integer3.java:19: error: cannot find symbol

int number1 = Integer.parseInt (input); //Used t

o convert from string to integer

^

symbol: method parseInt(String)

location: class Integer

Integer3.java:20: error: cannot find symbol

int number2 = Integer.parseInt (input2); //Used

to convert from string to integer

^

symbol: method parseInt(String)

location: class Integer

Integer3.java:24: error: userinput is already defined in Integer3()

String userinput = JOptionPane.showInputDialog (

"Do you want to continue? ");

^

.\IntegerPanel.java:52: error: cannot find symbol

number1 = Integer.parseInt (numberInput1);

^

symbol: method parseInt(String)

location: class Integer

.\IntegerPanel.java:53: error: cannot find symbol

nmber2 = Integer.parseInt (numberInput2);

^

symbol: variable nmber2

location: class IntegerPanel.CalculateListener

.\IntegerPanel.java:53: error: cannot find symbol

nmber2 = Integer.parseInt (numberInput2);

^

symbol: method parseInt(String)

location: class Integer

.\IntegerPanel.java:58: error: method toString in class Object cannot be applied

to given types;

outputSum.setText (Integer.toString (sum));

^

required: no arguments

found: int

reason: actual and formal argument lists differ in length

7 errors

Can anyone help with this program please?

Update:

But my one on the computer isn't working, even after a small change below, and the one you answer is fine.

2 Answers

Relevance
  • Silent
    Lv 7
    9 years ago
    Favorite Answer

    It compiles fine for me once I fix the double local variable error (you're declaring two local variables called "userinput" in the same scope). The other errors are probably caused by the compiler not being able to load some class it's looking for, or something.

    How are you compiling this — with the command line compiler, or some IDE? What does your classpath look like?

  • 9 years ago

    It did work nicely for me. See a small change at the end

    import javax.swing.JOptionPane;

    public class Integer3

    {

    public static void main (String args[])

    {

    new Integer3 ();

    }

    public Integer3()

    {

    //Get the user's first integer

    String userinput = "yes";

    while (userinput.equals("yes"))

    {

    String input = JOptionPane.showInputDialog ("Please enter the first integer>> ");

    String input2 = JOptionPane.showInputDialog ("Please enter the second integer>> ");

    int number1 = Integer.parseInt (input); //Used to convert from string to integer

    int number2 = Integer.parseInt (input2); //Used to convert from string to integer

    System.out.println ("The sum of the two numbers is: " + (number1 + number2));

    System.out.println ("The product of the two numbers is: " + (number1 * number2));

    System.out.println ();

    userinput = JOptionPane.showInputDialog ("Do you want to continue? ");//<------------- change

    }

    }

    }

Still have questions? Get your answers by asking now.