• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number Guesser Program  RSS feed

 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still new to programming I need help please.



This program executes very well as planned but it does not evaluate the condition (the if statement) whereby guess is equal to randomNumber. It carries on and outputs the last (else) statement insted.
For example if your guess was 4 and the randomNumber was also 4 it should say (YOu win) but it says (NO). Where is the problem?Number Guesser Program
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that guess will always be 0, because you never reassign it to the value that the user entered.
Hint: the showInputDialog() returns a String.
 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i do that?
 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was easy for me to run this program in JavaScript because I could reassign guess to the value that the user entered like this:

guess = prompt("Guess the number: ")

how should I do it in Java?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said, at line 21 JOptionPane.showInputDialog() returns a String, which contains the user input.
However, guess is an int so you can't directly assign this return value to it; the compiler won't let you.
You need to explicitly convert from String to int, which you can do using Integer.parseInt().
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thabo Ntsane wrote:It was easy for me to run this program in JavaScript because I could reassign guess to the value that the user entered like this:

guess = prompt("Guess the number: ")

how should I do it in Java?

Winston wrote a set of articles on how to Get User Input. Probably a pretty good place to start.
 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easier when I use the Scanner but in this case I want to use GUI

so i tried this:

guess = Interger.parseInt(JOPtionPane.showInputDialog("Guess the number: "));


Still doesnt work
 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it thanks a lot guys for your comments

guess = Integer.parseInt(JOptionPane.showInputDialog("Guess the number: "));


AMAZING!!!

 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Admittedly, I'm not the sharpest tool in the shed, but both lines appear to be the same. You post in one message that the code didn't work; then, in the next post, you show the same line of code and say that it worked.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the unusual spelling of Integer in the first post was a copying mistake. Obviously it was the actual problem.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!