Results 1 to 19 of 19
Thread: JOptionPane and Scanner
- 01-09-2013, 04:48 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 01-09-2013, 05:04 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
You need to learn yourself how to look such things up by yourself: Enter them into google and look for an example, there are hundreds - my third hit is a tutorial about it if I enter JOptionPane. Do that before asking such questions and you will find that it is much quicker. Only come here to ask specific questions regarding problems you encounter while using the examples you find on the net.
I like likes!.gif)
- 01-09-2013, 05:07 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 01-09-2013, 05:10 PM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
So you have the code to get the input from the user? That is something that is described in every tutorial regarding that topic...? (Google: e.g. "JOptionPane tutorial")
Show us and you save 4 posts and much of our time... so we can go on solving your real problem?
The steps are:
- Get input from user
- Convert userinput to integerLast edited by Sierra; 01-09-2013 at 05:17 PM.
I like likes!.gif)
- 01-09-2013, 05:29 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
New error has risen from the grave.
java.lang.NullPointerExceptionJava Code:import javax.swing.JOptionPane; public class UserNumber extends BasicGame { private java.util.Random randy; private int usersNumber; private int compNumber = 1 + randy.nextInt(100); public UserNumber(){ super(); randy = new java.util.Random(); } //======================= public void askUsersFirstChoice(){ String a = JOptionPane.showInputDialog ("Enter your number from 1 to 100."); usersNumber = Integer.parseInt(a); generateRandom(); } //======================= public void askUsersNextChoice(){ } public void showGuess(){ JOptionPane.showMessageDialog(null, compNumber); showUpdatedStatus(); } public void generateRandom(){ compNumber = 1 + randy.nextInt(100); } public void generateAnother(){ compNumber = 1 + randy.nextInt(100); while (compNumber == compNumber){ compNumber = 1 + randy.nextInt(100); } } public boolean shouldContinue(){ return usersNumber != compNumber; } //====================== public void showFinalStatus(){ JOptionPane.showMessageDialog (null, "The computer guessed right!"); } public void showUpdatedStatus(){ String update_q = JOptionPane.showInputDialog ("Is this your number? Enter 'Yes or 'No'"); if(update_q.equalsIgnoreCase("No")){ generateAnother(); } if(update_q.equalsIgnoreCase("Yes")){ showFinalStatus(); } String update = JOptionPane.showInputDialog ("Too high or low? Enter 'Too High or 'Too Low'"); if(update.equalsIgnoreCase("Too high")){ generateAnother(); } if(update.equalsIgnoreCase("Too low")){ generateAnother(); } } //======================= // inherited from BasicGame: // playManyGames // playOneGame // showFinalStatus }
at UserNumber.showUpdatedStatus(UserNumber.java:51)
at BasicGame.playOneGame(BasicGame.java:21)
at BasicGame.playManyGames(BasicGame.java:12)
at GameApp.main(GameApp.java:9)
java.lang.NullPointerException
at UserNumber.<init>(UserNumber.java:5)
at GameApp.main(GameApp.java:8)
I don't know what a null pointer exception is.Last edited by Army; 01-09-2013 at 05:50 PM.
- 01-09-2013, 10:11 PM #6
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
First of all I see an infinite loop that is surely not wanted:
while (compNumber == compNumber)
Your NullPointerException means that you are trying to access an object reference that does not refer to an object but to null. Example:
private java.util.Random randy;
private int usersNumber;
private int compNumber = 1 + randy.nextInt(100);
In the last line you try to access "randy" which you declared two lines before but you did not initialize it!
private java.util.Random randy = new Random();I like likes!.gif)
- 01-09-2013, 10:11 PM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
I got a stackoverflowError when running my program, BlueJ highlighted extends Object in the BasicGame class. Anyone have any thoughts why? Is it because I called an object from it's subclass UsersNumber?
Also to Sierra I put that loop there because I needed something to check if usersNumber has already been used to get generate another number. Do you have any suggestions to fix it?
Java Code:import javax.swing.JOptionPane; public class BasicGame extends Object { private String itsSecretWord = "duck"; private String itsUsersWord = "none"; private int mark; UserNumber user = new UserNumber(); public void playManyGames() { do { playOneGame(); }while (JOptionPane.showConfirmDialog (null, "again?") == JOptionPane.YES_OPTION); } //====================== public void playOneGame() { askUsersFirstChoice(); while (shouldContinue()) { showUpdatedStatus(); askUsersNextChoice(); mark++; if(mark == 7){ user.guessRightNumber(); } } showFinalStatus(); } //====================== public void askUsersFirstChoice() { itsUsersWord = JOptionPane.showInputDialog ("Guess the secret word:"); } //====================== public void askUsersNextChoice() { askUsersFirstChoice(); // no need to write the coding again } //====================== public boolean shouldContinue() { return ! itsSecretWord.equals (itsUsersWord); } //====================== public void showUpdatedStatus() { JOptionPane.showMessageDialog (null, "That was wrong. Hint: It quacks."); } //====================== public void showFinalStatus() { JOptionPane.showMessageDialog (null, "That was right. \nCongratulations."); } //====================== }Java Code:import javax.swing.JOptionPane; public class UserNumber extends BasicGame { private java.util.Random randy; private int usersNumber; private int compNumber; public UserNumber(){ super(); randy = new java.util.Random(); } //======================= public void askUsersFirstChoice(){ String a = JOptionPane.showInputDialog ("Enter your number from 1 to 100."); usersNumber = Integer.parseInt(a); generateRandom(); showGuess(); } //======================= public void askUsersNextChoice(){ generateAnother(); showGuess(); } public void showGuess(){ JOptionPane.showMessageDialog(null, compNumber); showUpdatedStatus(); } public void generateRandom(){ compNumber = 1 + randy.nextInt(100); } public void generateAnother(){ compNumber = 1 + randy.nextInt(100); while (compNumber == compNumber){ compNumber = 1 + randy.nextInt(100); } } public boolean shouldContinue(){ return usersNumber != compNumber; } //====================== public void showFinalStatus(){ JOptionPane.showMessageDialog (null, "The computer guessed right!"); } public void showUpdatedStatus(){ String update_q = JOptionPane.showInputDialog ("Is this your number? Enter 'Yes' or 'No'"); if(update_q.equalsIgnoreCase("No")){ generateAnother(); showUpdatedStatus(); } if(update_q.equalsIgnoreCase("Yes")){ showFinalStatus(); } String update = JOptionPane.showInputDialog ("Too high or low? Enter 'Too High or 'Too Low'"); if(update.equalsIgnoreCase("Too high")){ generateAnother(); showUpdatedStatus(); } if(update.equalsIgnoreCase("Too low")){ generateAnother(); showUpdatedStatus(); } } //======================= public void guessRightNumber(){ compNumber = usersNumber; showGuess(); } // inherited from BasicGame: // playManyGames // playOneGame // showFinalStatus }Last edited by Army; 01-09-2013 at 10:13 PM.
- 01-09-2013, 10:14 PM #8
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
You do not need to extend Object - every Java class extends Object by default.
I like likes!.gif)
- 01-09-2013, 10:15 PM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 01-09-2013, 10:20 PM #10
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
Your OVerflow results from a circular relation in your classes - UserNumber and BasicGame which does this:
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
UserNumber(BasicGame).<init>() line: 9
UserNumber.<init>() line: 12
[...]
When creating a BasicGame you do:
UserNumber user = new UserNumber();
And when creating a UserNumber you do:
public class UserNumber extends BasicGame
That you must not do... ^^
You must remove the extend BasicGame, it is not needed here and makes no sense.I like likes!.gif)
- 01-09-2013, 10:25 PM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
Thanks, that makes sense.
- 01-09-2013, 10:46 PM #12
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
As a matter of fact - when you remove the extends Object you also need of course to remove the super() ... :)
I like likes!.gif)
- 01-09-2013, 11:43 PM #13
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
When I run the program I run it and then it prints the computers number and if I say "No" or "Yes" it exits. Any thoughts on why it does that?
- 01-09-2013, 11:44 PM #14
Member
- Join Date
- Jan 2013
- Posts
- 8
- Rep Power
- 0
Re: JOptionPane and Scanner
You don't need to using the Scanner package. You need to use:
Java Code:int num = JOptionPane.showInputDialog("Input a number");
- 01-10-2013, 12:10 AM #15
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
It does not exit, it runs in an infinite loop I already told you above... read again my answer, #6 post in this thread.
I like likes!.gif)
- 01-10-2013, 12:14 AM #16
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
Changed it to an if statement and it works perfect!
Last edited by Army; 01-10-2013 at 12:21 AM.
- 01-10-2013, 01:08 AM #17
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: JOptionPane and Scanner
ARGH this is the whole thing I have to do:
Write a subclass of BasicGame in which the computer asks the user to choose a whole number from 1 to 100, inclusive. The program then makes a number of guesses. After each guess, the user tells whether the guess is too high, too low, or exactly right. Once the computer "knows" what the correct number is, the computer tells the user the correct number and the number of guesses tried. Have the program ask the user after each game whether she wants to play again. Design the program so that it always guesses the correct answer by the seventh try at latest. Hint: Have two instance variables that keep track of the smallest and largest values that the guess could be (initially 1 and 100). Guess halfway between them. After each wrong guess, update these smallest and largest values appropriately.
When I run it everything is fine except two things:
1. It doesn't guess higher or lower based on your input of 'Too high' or 'Too low'.
2. It doesn't guess right after 7 (2 for faster testing).
Who ever came up with this project sucks :'(Java Code:import javax.swing.JOptionPane; public class UserNumber extends BasicGame { private java.util.Random randy; private int usersNumber; private int compNumber; private int mark; public void playManyGames() { do { playOneGame(); }while (JOptionPane.showConfirmDialog (null, "again?") == JOptionPane.YES_OPTION); } //====================== public void playOneGame() { askUsersFirstChoice(); while (shouldContinue()) { showUpdatedStatus(); askUsersNextChoice(); } if(mark == 2){ guessRightNumber(); } showFinalStatus(); } //====================== public UserNumber(){ super(); randy = new java.util.Random(); } //======================= public void askUsersFirstChoice(){ String a = JOptionPane.showInputDialog ("Enter your number from 1 to 100."); usersNumber = Integer.parseInt(a); generateRandom(); showGuess(); } //======================= public void askUsersNextChoice(){ generateRandom(); showGuess(); } public void showGuess(){ JOptionPane.showMessageDialog(null, compNumber); showUpdatedStatus(); } public void generateRandom(){ compNumber = 1 + randy.nextInt(100); } public void generateAnother(){ compNumber = 1 + randy.nextInt(100); if (compNumber == compNumber){ compNumber = 1 + randy.nextInt(100); } } public boolean shouldContinue(){ boolean valueToReturn = usersNumber != compNumber; return valueToReturn; } //====================== public void showFinalStatus(){ JOptionPane.showMessageDialog (null, "The computer guessed right in " + mark + " guesses!"); } public void showUpdatedStatus(){ String update_q = JOptionPane.showInputDialog ("Is this your number? Enter 'Yes' or 'No'"); if(update_q.equalsIgnoreCase("No")){ guessCorrector(); } if(update_q.equalsIgnoreCase("Yes")){ showFinalStatus(); } } //======================= public void guessCorrector(){ String update = JOptionPane.showInputDialog ("Too high or low? Enter 'Too High or 'Too Low'"); if(update.equalsIgnoreCase("Too high")){ mark++; compNumber = compNumber - 10; askUsersNextChoice(); } if(update.equalsIgnoreCase("Too low")){ mark++; compNumber = compNumber + 10; askUsersNextChoice(); } } public void guessRightNumber(){ compNumber = usersNumber; showGuess(); } // inherited from BasicGame: // playManyGames // playOneGame // showFinalStatus }
- 01-10-2013, 01:31 AM #18
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: JOptionPane and Scanner
Of course, because after lowering your number you overwrite it by a call to generateRandom() from askUsersNextChoice()
So the compNumber gets overwritten by a new random number... also you will run into the problem that you might jump between high and low if the number is in between the two, just as a hint.I like likes!.gif)
- 01-10-2013, 02:29 AM #19
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Similar Threads
-
Problem using Scanner and JOptionPane simultaneously
By mitchj in forum New To JavaReplies: 3Last Post: 08-20-2012, 07:57 PM -
JOptionPane Help
By adjit in forum New To JavaReplies: 23Last Post: 01-04-2012, 02:20 PM -
Converting Scanner input to JOptionPane
By Mideoan in forum New To JavaReplies: 1Last Post: 03-09-2011, 11:26 PM -
need help with a JOptionPane
By dr4g0nk1ng in forum Advanced JavaReplies: 2Last Post: 02-19-2010, 09:40 PM -
JOptionpane
By tommyyyy in forum New To JavaReplies: 2Last Post: 03-20-2009, 08:33 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks