Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By Sierra

Thread: JOptionPane and Scanner

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default JOptionPane and Scanner

    Is there a way to use the Scanner package and the JOptionPane? I want to have a user input a number and then put that number to an int.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: JOptionPane and Scanner

    Quote Originally Posted by Sierra View Post
    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.
    Funny thing because I have and none helps me with the problems I have now.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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 integer
    Last edited by Sierra; 01-09-2013 at 05:17 PM.
    I like likes!

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: JOptionPane and Scanner

    New error has risen from the grave.
    Java 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
    }
    java.lang.NullPointerException
    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.

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  7. #7
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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.

  8. #8
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: JOptionPane and Scanner

    You do not need to extend Object - every Java class extends Object by default.
    I like likes!

  9. #9
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: JOptionPane and Scanner

    Quote Originally Posted by Sierra View Post
    You do not need to extend Object - every Java class extends Object by default.
    Yes, I know, but that is what the book's class said to do. When I take it out it just highlights the super(); in my constructor and continues the stackoverflowerror

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    Army likes this.
    I like likes!

  11. #11
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: JOptionPane and Scanner

    Thanks, that makes sense.

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  13. #13
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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?

  14. #14
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default 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");

  15. #15
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  16. #16
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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.

  17. #17
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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).

    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
    }
    Who ever came up with this project sucks :'(

  18. #18
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  19. #19
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: JOptionPane and Scanner

    Quote Originally Posted by Sierra View Post
    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.
    Yeah, right as I did your test it did the whole ping pong thing. Do you have any solutions to why my guessRightNumber() method doesn't work?

Similar Threads

  1. Replies: 3
    Last Post: 08-20-2012, 07:57 PM
  2. JOptionPane Help
    By adjit in forum New To Java
    Replies: 23
    Last Post: 01-04-2012, 02:20 PM
  3. Converting Scanner input to JOptionPane
    By Mideoan in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 11:26 PM
  4. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 09:40 PM
  5. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •