Results 1 to 15 of 15
- 01-07-2012, 09:38 PM #1
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
-
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.
- 01-07-2012, 10:09 PM #3
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.
- 01-07-2012, 11:51 PM #4
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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 MethodLast edited by Norm; 01-07-2012 at 11:57 PM. Reason: fixed code tag
- 01-07-2012, 11:58 PM #5
Re: Secondary input bug within times tables game,
Where is the loop?I now have an infinite loop
Show the program's output
- 01-08-2012, 12:05 AM #6
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
- 01-08-2012, 12:07 AM #7
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.
- 01-08-2012, 12:10 AM #8
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
- 01-08-2012, 12:13 AM #9
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?
- 01-08-2012, 12:20 AM #10
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
- 01-08-2012, 10:01 PM #11
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
- 01-08-2012, 10:04 PM #12
Re: Secondary input bug within times tables game,
What program is printing that message? How are you executing the program? Some IDEs can add extra messages.Is the red java 13 supossed to print?
- 01-08-2012, 10:13 PM #13
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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.
- 01-08-2012, 11:22 PM #14
Re: Secondary input bug within times tables game,
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 :DIm using netbeans 6.9.1, Im not sure what you mean by "how are you executing the program?" can you explain this please?
If that is the case, norm is asking about what the console output was from netbeans, in its entirety please!
- 01-09-2012, 01:56 AM #15
Member
- Join Date
- Nov 2011
- Location
- Belfast
- Posts
- 37
- Rep Power
- 0
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
-
Need help with secondary sort
By yankeefan9874 in forum New To JavaReplies: 4Last Post: 04-19-2011, 08:27 AM -
Secondary JFrame
By jitheshmenon in forum AWT / SwingReplies: 7Last Post: 08-09-2010, 11:56 PM -
Displaying Multiple Tables based on Input
By Berzerk in forum New To JavaReplies: 11Last Post: 06-17-2010, 07:54 AM -
Java not recognizing UNIX secondary group
By PaulBinni in forum NetworkingReplies: 1Last Post: 11-18-2009, 03:45 PM -
changing the secondary ID of a view
By schuetzejanett in forum SWT / JFaceReplies: 0Last Post: 08-08-2007, 07:28 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks