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 10 of 10

Thread: I am doing my homework =D

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

    Default I am doing my homework =D

    Hi I just had a quick question. I have this code

    if (answer > num1)
    JOptionPane.showMessageDialog(null,"Your answer is too high");
    else if (answer < num1)
    JOptionPane.showMessageDialog(null, "Your answer is too low");
    else
    JOptionPane.showMessageDialog(null, "You won!");

    and its gave me this as an error.
    error:bad operand types for binary operator '>'
    error:bad operand types for binary operator '<'

    What do those errors mean?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am doing my homework =D

    If you have a question, provide an SSCCE that demonstrates what you're talking about.

    For example, what are answer and num1? Are they ints? Chars? Strings? Something else? What are their values?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am doing my homework =D

    They are this
    int num1 = (int) (System.currentTimeMillis()%10);

    String answer = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am doing my homework =D

    Okay, so answer is a String. How can a String be less than or greater than a number? Looks like you need to convert that to an int.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: I am doing my homework =D

    I guess that would make sense to convert into an int.

    How about this?
    String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
    int answer = Integer.parseInt(answerString);

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am doing my homework =D

    Quote Originally Posted by valenciajv View Post
    I guess that would make sense to convert into an int.

    How about this?
    String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
    int answer = Integer.parseInt(answerString);
    You tell me! What happened when you tried it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am doing my homework =D

    It said process completed. =D
    Thank you.
    I am not done with the whole thing yet. Time to resume my homework. =D

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am doing my homework =D

    Quote Originally Posted by valenciajv View Post
    It said process completed. =D
    Thank you.
    I am not done with the whole thing yet. Time to resume my homework. =D
    Well you can always test that piece by adding some print statements, or trying to add numbers to it or by using the < or > operators, and seeing if the results are what you'd expect. A good rule of thumb while programming is to always test the current step before moving on to the next one. That way you always know where any bugs are. But I have a feeling you've got this step down!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am doing my homework =D

    Does this make a random number between 1 and 10?
    I never exactly knew how this one works cause I would get zeros as well. Can you help?

    int num1 = (int) (System.currentTimeMillis()%10);

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am doing my homework =D

    Quote Originally Posted by valenciajv View Post
    Does this make a random number between 1 and 10?
    I never exactly knew how this one works cause I would get zeros as well. Can you help?

    int num1 = (int) (System.currentTimeMillis()%10);
    The answer to most of these kinds of questions is going to be- what happened when you tried it? System.out.println() is your best friend. Use it.

    You might also want to look at the Math or Random classes. The API is your other best friend: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 11:09 AM
  2. need help with homework program...thanks
    By robertsbd in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 5th, 2010, 03:12 PM
  3. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  4. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM