Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can't fix my own errors

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Can't fix my own errors

    Hello to all! First I would like to say that I am only a very real beginner. IN fact, I have recently started taking a Java Programming class, but have found it very interesting. Now I have messing around with a code. And for some reason, I can not get the problem solved. I have completed the same code but without the JOptionPane.showDialogBox.

    My error message is


    MarcsMac:cmis141 marcroberts$ javac RandomNumbers.java
    RandomNumbers.java:21: cannot find symbol
    symbol : variable answer
    location: class RandomNumbers
    if (one + two + three == answer)
    ^
    Here is my code:


    import javax.swing.JOptionPane;

    public class RandomNumbers {
    public static void main (String[] args) {

    //Generate three random numbers
    int one, two, three;
    one = (int)(Math.random()*(10));
    two = (int)(Math.random()*(10));
    three = (int)(Math.random()*(10));

    //What is the sum of these numbers
    String sumOfNumbersString =
    JOptionPane.showInputDialog(null,"What is the sum of " + one + "+" +
    two + "+" + three + "?");

    //Convert string to int
    int sumOfNumbers =Integer.parseInt(sumOfNumbersString);

    //Check the answer and Display Results
    if (one + two + three == answer)
    JOptionPane.showMessageDialog(null, "You are Correct!");
    else
    JOptionPane.showMessageDialog(null, "Sorry, that is Incorrect \n" +
    one + "+" + two + "+" + three + " equals " + (one + two + three));

    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't fix my own errors

    Did you bother to read the error message? It says that it can not find the variable called answer. I can't either. Can you?

  3. The Following User Says Thank You to Junky For This Useful Post:

    mrroberts2u (April 14th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Wink Re: Can't fix my own errors

    Ok so, I got the right code finally


    mport javax.swing.JOptionPane;

    public class RandomNumbers {
    public static void main (String[] args) {

    //Generate three random numbers
    int one, two, three;
    one = (int)(Math.random()*(10));
    two = (int)(Math.random()*(10));
    three = (int)(Math.random()*(10));

    //What is the sum of these numbers
    String sumOfNumbersString =
    JOptionPane.showInputDialog(null,"What is the sum of " + one + "+" +
    two + "+" + three + "?");

    //Convert string to int
    int sumOfNumbers =Integer.parseInt(sumOfNumbersString);

    //Check the answer and Display Results
    if (one + two + three == answer)
    JOptionPane.showMessageDialog(null, "You are Correct!");
    else
    JOptionPane.showMessageDialog(null, "Sorry, that is Incorrect \n" +
    one + "+" + two + "+" + three + " equals " + (one + two + three));

    }
    }


    I simply changed == answer to sumOfNumbers and all worked wonderfully!!

  5. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Can't fix my own errors

    Quote Originally Posted by mrroberts2u View Post
    Ok so, I got the right code finally
    ...
    I simply changed == answer to sumOfNumbers and all worked wonderfully!!
    So why don't you post the corrected code? Why post the bad code again?

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can't fix my own errors

    import javax.swing.JOptionPane;

    public class RandomNumbers {
    public static void main (String[] args) {

    //Generate three random numbers
    int one, two, three;
    one = (int)(Math.random()*(10));
    two = (int)(Math.random()*(10));
    three = (int)(Math.random()*(10));

    //What is the sum of these numbers
    String sumOfNumbersString =
    JOptionPane.showInputDialog(null,"What is the sum of " + one + "+" +
    two + "+" + three + "?");

    //Convert string to int
    int sumOfNumbers =Integer.parseInt(sumOfNumbersString);

    //Check the answer and Display Results
    if (one + two + three == sumOfNumbers)
    JOptionPane.showMessageDialog(null, "You are Correct!");
    else
    JOptionPane.showMessageDialog(null, "Sorry, that is Incorrect \n" +
    one + "+" + two + "+" + three + " equals " + (one + two + three));

    }
    }

Similar Threads

  1. GUI errors
    By cindisue in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2011, 09:54 AM
  2. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  3. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  4. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM
  5. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 07:04 PM