Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Norm

Thread: Secondary input bug within times tables game,

  1. #1
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Secondary input bug within times tables game,

    Hello,

    Im having a "secondary input bug" (dont know how else to describe it). I have created a times table program -

    - Option 1 Show the 5's times tables.
    - Option 2 Play the 5's times tables.
    - Option 3 Answer random times table questions
    - Option 4 Exit program.

    Im sure all the vetran programmers know this one. The problem occurs after selecting option 1 for the second time, you select it option one and then select 5 to show the 5 times tables the cycle then finishes then you are presented with the menu again and you select option 1 but this time you put a non-numerical value i.e. kljbnkadba, from there the try/catch statement kicks in but the program also goes on and prints out the 5 times tables again.

    I put a System.exit(1); after the catch but it brings up red writing Java result ##

    The easiest way to see what I mean is to run the code, its a tricky one to explain.

    Does anyone have any solutions for this?

    All ideas are welcome

    Thanks

    Martyn


    Results

    run:
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    5
    5 * 1 = 5
    5 * 2 = 10
    5 * 3 = 15
    5 * 4 = 20
    5 * 5 = 25
    5 * 6 = 30
    5 * 7 = 35
    5 * 8 = 40
    5 * 9 = 45
    5 * 10 = 50
    5 * 11 = 55
    5 * 12 = 60
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    marty
    Invalid data entry, please try again Try Catch ststement kicking in
    5 * 1 = 5
    5 * 2 = 10
    5 * 3 = 15
    5 * 4 = 20
    5 * 5 = 25
    5 * 6 = 30
    5 * 7 = 35
    5 * 8 = 40
    5 * 9 = 45
    5 * 10 = 50
    5 * 11 = 55
    5 * 12 = 60
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit



    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package testing;
    
    /**
     *
     * @author pc user
     */
    import java.util.Scanner;
    
    public class Testing {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            //Scan for user input
            Scanner scanner = new Scanner(System.in);
    
            // Initialise variables
            int option = 0;
            int choice = 0;
            int userAnswer = 0;
            int answer = 0;
            int exactAnswer = 0;
            int i = 0;
            int randomNumber1 = 0;
            int randomNumber2 = 0;
            int correctAnswer = 0;
            int correctCrt = 0;
    
            //Output option menu to user
            do {
                System.out.println("Multiplication tables game\n");
                System.out.println("1... Times tables - (select and view)");
                System.out.println("2... Practice - (select and practice)");
                System.out.println("3... New game - (12 random times table sums)");
                System.out.println("4... Quit");
    
                //Try non-numerical data entries
                try {
    
                    //read in the value choosen
                    option = scanner.nextInt();
    
                    //Catch non-numerical data entries
                } catch (Exception ex) {
                    System.out.println("Invalid data entry, please try again ");
                    
                }
                //Initialise option selection
                switch (option) {
                    /**
                     * Option/Case 1 enables the user to select a times table to view i.e enter 5 to
                     * view the 5's times tables.
                     */
                    case 1://Initialise option 1 if user inputs option 1
                        //Try non-numerical data entries
                        try {
                            //Scan for user input after option 1 has been selected
                            Scanner case1 = new Scanner(System.in);
    
                            //Output statement below once option 1 hase
                            System.out.println("Select times table to view (Between 1 and 12 only)...");
    
                            //Read in user input
                            choice = case1.nextInt();
    
                            //Catch non-numerical data entries
                        } catch (Exception ex) {
                            System.out.println("Invalid data entry, please try again ");
                            //System.exit(1);
                        }
    
                        //Make sure the number entered falls within the limits of 1 & 12
                        if (choice >= 1 && choice <= 12) { //Enter code to write multiplication selected
    
                            //Loop to execute code and output statement
                            for (i = 1; i <= 12; i++) {
                                System.out.println(choice + " * " + i + " = " + choice * i);
                            }
                        }
                        if (choice <= 0 || choice >= 13) {
                            System.out.println("Please enter a number within the range of 1 & 12");
                        }
                        continue;//Return to start menu when cycle has completed
    
                      case 4:
                        option = 4; //Initialise option 4 if user inputs option 4
                        //Output statement when user selects option 4
                        System.out.println("Thank you for playing bye...");
                        continue;      //End program if user selects option 4
    
                    //Output statement if user enters invalid selection
                    default:
                        System.out.println("Please enter a valid option");
                }
            } while (option != 4);
        }//End of Main Method
    }//End of Class Method

    Any help would be great on this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Secondary input bug within times tables game,

    Why are you using more than one Scanner object? I think you would be far better off sticking to only one for this application.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Secondary input bug within times tables game,

    Remember from your earlier posting of problems with this program, you need to call nextLine() after calls to nextInt() to clear out Scanner's buffer.

    You should call the printStackTrace() method in your catch block so you know exactly what the exception is.

    What do you want the program to do when there is an exception?
    Last edited by Norm; 01-07-2012 at 10:11 PM.
    Martyn likes this.

  4. #4
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Hello gents,

    Thanks for the feedback, I tried both of your suggestions and I now have an infinite loop, any thoughts?

    Thanks

    Martyn




    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package testing;
    
    /**
     *
     * @author pc user
     */
    import java.util.Scanner;
    
    public class Testing {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            //Scan for user input
            Scanner scanner = new Scanner(System.in);
    
            // Initialise variables
            int option = 0;
            int choice = 0;
            int userAnswer = 0;
            int answer = 0;
            int exactAnswer = 0;
            int i = 0;
            int randomNumber1 = 0;
            int randomNumber2 = 0;
            int correctAnswer = 0;
            int correctCrt = 0;
    
            //Output option menu to user
            do {
                System.out.println("Multiplication tables game\n");
                System.out.println("1... Times tables - (select and view)");
                System.out.println("2... Practice - (select and practice)");
                System.out.println("3... New game - (12 random times table sums)");
                System.out.println("4... Quit");
    
                //Try non-numerical data entries
                try {
    
                    //read in the value choosen
                    option = scanner.nextInt();
                    scanner.nextLine();
    
                    //Catch non-numerical data entries
                } catch (Exception ex) {
                    System.out.println("Invalid data entry, please try again ");
    
                }
                //Initialise option selection
                switch (option) {
                    /**
                     * Option/Case 1 enables the user to select a times table to view i.e enter 5 to
                     * view the 5's times tables.
                     */
                    case 1://Initialise option 1 if user inputs option 1
                        //Try non-numerical data entries
                        try {
                            //Scan for user input after option 1 has been selected
                            //Scanner case1 = new Scanner(System.in);
    
                            //Output statement below once option 1 hase
                            System.out.println("Select times table to view (Between 1 and 12 only)...");
    
                            //Read in user input
                            choice = scanner.nextInt();
                            scanner.nextLine();
    
                            //Catch non-numerical data entries
                        } catch (Exception ex) {
                            System.out.println("Invalid data entry, please try again ");
                            //System.exit(1);
                        }
    
                        //Make sure the number entered falls within the limits of 1 & 12
                        if (choice >= 1 && choice <= 12) { //Enter code to write multiplication selected
    
                            //Loop to execute code and output statement
                            for (i = 1; i <= 12; i++) {
                                System.out.println(choice + " * " + i + " = " + choice * i);
                            }
                        }
                        if (choice <= 0 || choice >= 13) {
                            System.out.println("Please enter a number within the range of 1 & 12");
                        }
                        continue;//Return to start menu when cycle has completed
    
                      case 4:
                        option = 4; //Initialise option 4 if user inputs option 4
                        //Output statement when user selects option 4
                        System.out.println("Thank you for playing bye...");
                        continue;      //End program if user selects option 4
    
                    //Output statement if user enters invalid selection
                    default:
                        System.out.println("Please enter a valid option");
                }
            } while (option != 4);
        }//End of Main Method
    }//End of Class Method
    Last edited by Norm; 01-07-2012 at 11:57 PM. Reason: fixed code tag

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Secondary input bug within times tables game,

    I now have an infinite loop
    Where is the loop?

    Show the program's output

  6. #6
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Hi Norm heres the first five outputs, thanks

    run:
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    marty
    Invalid data entry, please try again
    Please enter a number within the range of 1 & 12
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    Invalid data entry, please try again
    Select times table to view (Between 1 and 12 only)...
    Invalid data entry, please try again
    Please enter a number within the range of 1 & 12
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    Invalid data entry, please try again
    Select times table to view (Between 1 and 12 only)...
    Invalid data entry, please try again
    Please enter a number within the range of 1 & 12
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    Invalid data entry, please try again
    Select times table to view (Between 1 and 12 only)...
    Invalid data entry, please try again
    Please enter a number within the range of 1 & 12
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    Invalid data entry, please try again
    Select times table to view (Between 1 and 12 only)...
    Invalid data entry, please try again
    Please enter a number within the range of 1 & 12

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Secondary input bug within times tables game,

    Are you clearing the Scanner class's buffer so that the invalid data is removed and does not trigger another exception when you use nextInt with invalid input? I mentioned using nextLine() earlier as a means to clear the buffer.

  8. #8
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Hi Norm,

    I added -

    option = scanner.nextInt();
    scanner.nextLine();

    But it doesnt seem to be clearing the buffer

    Have I done this right?

    Thanks

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Secondary input bug within times tables game,

    What line in the program is throwing the exception?
    How many calls to the nextInt() method were made before the exception?
    Do all of those calls have a call to nextLine()?
    What if the use inputs bad data? How and where are you going to clear it?

  10. #10
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Hi Norm thanks for your help I will have to investigate the code a bit further to see/understand what is going on i will post online either way.

    Thanks again

    Martyn

  11. #11
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Hi,

    I think I have fixed the cycle problem by adding System.exit(); after the try/catch statements the output is now -

    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    2
    2 * 1 = 2
    2 * 2 = 4
    2 * 3 = 6
    2 * 4 = 8
    2 * 5 = 10
    2 * 6 = 12
    2 * 7 = 14
    2 * 8 = 16
    2 * 9 = 18
    2 * 10 = 20
    2 * 11 = 22
    2 * 12 = 24
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    jimbob
    Invalid data entry, please try again
    Java Result: 13
    BUILD SUCCESSFUL (total time: 9 seconds)


    Is the red java 13 supossed to print?

    Is this acceptable?

    Thanks

    Martyn

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Secondary input bug within times tables game,

    Is the red java 13 supossed to print?
    What program is printing that message? How are you executing the program? Some IDEs can add extra messages.

  13. #13
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Im using netbeans 6.9.1, Im not sure what you mean by "how are you executing the program?" can you explain this please?
    Last edited by Martyn; 01-08-2012 at 10:17 PM.

  14. #14
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Secondary input bug within times tables game,

    Im using netbeans 6.9.1, Im not sure what you mean by "how are you executing the program?" can you explain this please?
    Some people use the command line, some use an application server, some use an IDE. I think that is what we were getting at. You're executing via the big green play button in netbeans I assume :D

    If that is the case, norm is asking about what the console output was from netbeans, in its entirety please!

  15. #15
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Secondary input bug within times tables game,

    Thanks for the feedback this is the output in its entirety -

    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit.
    1
    Select times table to view (Between 1 and 12 only)...
    2
    2 * 1 = 2
    2 * 2 = 4
    2 * 3 = 6
    2 * 4 = 8
    2 * 5 = 10
    2 * 6 = 12
    2 * 7 = 14
    2 * 8 = 16
    2 * 9 = 18
    2 * 10 = 20
    2 * 11 = 22
    2 * 12 = 24
    Multiplication tables game

    1... Times tables - (select and view)
    2... Practice - (select and practice)
    3... New game - (12 random times table sums)
    4... Quit
    1
    Select times table to view (Between 1 and 12 only)...
    jimbob
    Invalid data entry, please try again
    Java Result: 13
    BUILD SUCCESSFUL (total time: 9 seconds)
    Last edited by Martyn; 01-09-2012 at 01:48 PM.

Similar Threads

  1. Need help with secondary sort
    By yankeefan9874 in forum New To Java
    Replies: 4
    Last Post: 04-19-2011, 08:27 AM
  2. Secondary JFrame
    By jitheshmenon in forum AWT / Swing
    Replies: 7
    Last Post: 08-09-2010, 11:56 PM
  3. Displaying Multiple Tables based on Input
    By Berzerk in forum New To Java
    Replies: 11
    Last Post: 06-17-2010, 07:54 AM
  4. Java not recognizing UNIX secondary group
    By PaulBinni in forum Networking
    Replies: 1
    Last Post: 11-18-2009, 03:45 PM
  5. changing the secondary ID of a view
    By schuetzejanett in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2007, 07:28 PM

Posting Permissions

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