DeafBox
asked on
Non-numeric data
I want to make my input boxes only accept numeric input data. And if a user enters non-numeric data it loops the input box until they enter numeric data.
Also can someone tell me why my cancel button comes up with a compiler error.
Thankyou
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Vector;
public class Calc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
int i=0;
int userChoice = 0;
double playerChoice = 0;
String userChoiceStr;
String playerChoiceStr;
// Vector v = new Vector ();
double v[] = new double[50];
playerChoiceStr = JOptionPane.showInputDialo g(null,
"How many players?");
playerChoice = Double.parseDouble (playerChoiceStr);
for (i =0; i < (int)playerChoice ; i++)
{
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo g (null,
"Player" + " " + (i+1)
+ "\n" +
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA GE);
// keeps track of wrong entry
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDia log(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE );
}
}
while ( userChoice != 4 ); // returns your percentage if input not equal to 4
String str = "Got " + (result / count) + "% right!! \n";
v[i]= (result/count);
JOptionPane.showMessageDia log ( null , "You " + str );
}
String showAll = "";
i=0;
for (i =0; i < (int)playerChoice ; i++)
{
showAll = showAll + "Player "+ (i+1) + " " + v[i] +" % right!! \n";
}
JOptionPane.showMessageDia log ( null , showAll );
// Ends program
System.exit(0);
}
public static void addition()
{
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectAddition = JOptionPane.showInputDialo g ("What is" + " " + genNumOne +
" " + "plus" + " " + genNumTwo + "?");
additionData = Integer.parseInt(selectAdd ition);
if (additionData == (genNumOne + genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void subtraction()
{
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) ( Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
selectSubtraction = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "minus" + " " + genNumTwo + "?");
subtractionData = Integer.parseInt(selectSub traction);
if (subtractionData == (genNumOne - genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void multiplication()
{
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectMultiplication = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "times" + " " + genNumTwo + "?");
multiplicationData = Integer.parseInt(selectMul tiplicatio n);
if ( multiplicationData == (genNumOne * genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void correctAnswer()
{
String[] arrGood = { "Very good!", "Well done", "Great!" };
Random randGood = new Random();
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDia log(null, message,
"Good Answer",
JOptionPane.INFORMATION_ME SSAGE);
}
public static void incorrectAnswer()
{
String[] arrBad = { "Sorry that was incorrect.", "Better luck next time", "Bad luck" };
Random randBad = new Random();
int new_rand_index = randBad.nextInt( arrBad.length );
String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDia log(null, message,
"Bad Answer",
JOptionPane.INFORMATION_ME SSAGE);
}
}
Also can someone tell me why my cancel button comes up with a compiler error.
Thankyou
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Vector;
public class Calc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
int i=0;
int userChoice = 0;
double playerChoice = 0;
String userChoiceStr;
String playerChoiceStr;
// Vector v = new Vector ();
double v[] = new double[50];
playerChoiceStr = JOptionPane.showInputDialo
"How many players?");
playerChoice = Double.parseDouble (playerChoiceStr);
for (i =0; i < (int)playerChoice ; i++)
{
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo
"Player" + " " + (i+1)
+ "\n" +
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA
// keeps track of wrong entry
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE );
}
}
while ( userChoice != 4 ); // returns your percentage if input not equal to 4
String str = "Got " + (result / count) + "% right!! \n";
v[i]= (result/count);
JOptionPane.showMessageDia
}
String showAll = "";
i=0;
for (i =0; i < (int)playerChoice ; i++)
{
showAll = showAll + "Player "+ (i+1) + " " + v[i] +" % right!! \n";
}
JOptionPane.showMessageDia
// Ends program
System.exit(0);
}
public static void addition()
{
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectAddition = JOptionPane.showInputDialo
" " + "plus" + " " + genNumTwo + "?");
additionData = Integer.parseInt(selectAdd
if (additionData == (genNumOne + genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void subtraction()
{
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) ( Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
selectSubtraction = JOptionPane.showInputDialo
" " + "minus" + " " + genNumTwo + "?");
subtractionData = Integer.parseInt(selectSub
if (subtractionData == (genNumOne - genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void multiplication()
{
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectMultiplication = JOptionPane.showInputDialo
" " + "times" + " " + genNumTwo + "?");
multiplicationData = Integer.parseInt(selectMul
if ( multiplicationData == (genNumOne * genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void correctAnswer()
{
String[] arrGood = { "Very good!", "Well done", "Great!" };
Random randGood = new Random();
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDia
"Good Answer",
JOptionPane.INFORMATION_ME
}
public static void incorrectAnswer()
{
String[] arrBad = { "Sorry that was incorrect.", "Better luck next time", "Bad luck" };
Random randBad = new Random();
int new_rand_index = randBad.nextInt( arrBad.length );
String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDia
"Bad Answer",
JOptionPane.INFORMATION_ME
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had userChoice = Integer.parseInt(userChoic eStr); in my original code. I have no idea why it wasn't here when i posted.
ASKER
Got most of it working except one input.
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Vector;
public class Calc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
boolean inputOK = false;
int i=0;
int userChoice = 0;
double playerChoice = 0;
String userChoiceStr ;
String playerChoiceStr;
// Vector v = new Vector ();
double v[] = new double[50];
while (!inputOK)
{
try
{
playerChoiceStr = JOptionPane.showInputDialo g(null,
"How many players?");
playerChoice = Double.parseDouble (playerChoiceStr);
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
for (i =0; i < (int)playerChoice ; i++)
{
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo g (null,
"Player" + " " + (i+1)
+ "\n" +
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA GE);
// keeps track of wrong entry
userChoice = Integer.parseInt(userChoic eStr);
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
}
}
while ( userChoice != 4 ); // returns your percentage if input not equal to 4
String str = "Got " + (result / count) + "% right!! \n";
v[i]= (result/count);
JOptionPane.showMessageDia log ( null , "You " + str );
}
String showAll = "";
i=0;
for (i =0; i < (int)playerChoice ; i++)
{
showAll = showAll + "Player "+ (i+1) + " " + v[i] +" % right!! \n";
}
JOptionPane.showMessageDia log ( null , showAll );
// Ends program
System.exit(0);
}
public static void addition()
{
boolean inputOK = false;
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
while (!inputOK)
{
try
{
selectAddition = JOptionPane.showInputDialo g ("What is" + " " + genNumOne +
" " + "plus" + " " + genNumTwo + "?");
additionData = Integer.parseInt(selectAdd ition);
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if (additionData == (genNumOne + genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void subtraction()
{
boolean inputOK = false;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) ( Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
while (!inputOK)
{
try
{
selectSubtraction = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "minus" + " " + genNumTwo + "?");
subtractionData = Integer.parseInt(selectSub traction);
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if (subtractionData == (genNumOne - genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void multiplication()
{
boolean inputOK = false;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
while (!inputOK)
{
try
{
selectMultiplication = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "times" + " " + genNumTwo + "?");
multiplicationData = Integer.parseInt(selectMul tiplicatio n);
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if ( multiplicationData == (genNumOne * genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void correctAnswer()
{
String[] arrGood = { "Very good!", "Well done", "Great!" };
Random randGood = new Random();
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDia log(null, message,
"Good Answer",
JOptionPane.INFORMATION_ME SSAGE);
}
public static void incorrectAnswer()
{
String[] arrBad = { "Sorry that was incorrect.", "Better luck next time", "Bad luck" };
Random randBad = new Random();
int new_rand_index = randBad.nextInt( arrBad.length );
String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDia log(null, message,
"Bad Answer",
JOptionPane.INFORMATION_ME SSAGE);
}
}
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Vector;
public class Calc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
boolean inputOK = false;
int i=0;
int userChoice = 0;
double playerChoice = 0;
String userChoiceStr ;
String playerChoiceStr;
// Vector v = new Vector ();
double v[] = new double[50];
while (!inputOK)
{
try
{
playerChoiceStr = JOptionPane.showInputDialo
"How many players?");
playerChoice = Double.parseDouble (playerChoiceStr);
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
for (i =0; i < (int)playerChoice ; i++)
{
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo
"Player" + " " + (i+1)
+ "\n" +
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA
// keeps track of wrong entry
userChoice = Integer.parseInt(userChoic
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
}
}
while ( userChoice != 4 ); // returns your percentage if input not equal to 4
String str = "Got " + (result / count) + "% right!! \n";
v[i]= (result/count);
JOptionPane.showMessageDia
}
String showAll = "";
i=0;
for (i =0; i < (int)playerChoice ; i++)
{
showAll = showAll + "Player "+ (i+1) + " " + v[i] +" % right!! \n";
}
JOptionPane.showMessageDia
// Ends program
System.exit(0);
}
public static void addition()
{
boolean inputOK = false;
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
while (!inputOK)
{
try
{
selectAddition = JOptionPane.showInputDialo
" " + "plus" + " " + genNumTwo + "?");
additionData = Integer.parseInt(selectAdd
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if (additionData == (genNumOne + genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void subtraction()
{
boolean inputOK = false;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) ( Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
while (!inputOK)
{
try
{
selectSubtraction = JOptionPane.showInputDialo
" " + "minus" + " " + genNumTwo + "?");
subtractionData = Integer.parseInt(selectSub
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if (subtractionData == (genNumOne - genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void multiplication()
{
boolean inputOK = false;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
while (!inputOK)
{
try
{
selectMultiplication = JOptionPane.showInputDialo
" " + "times" + " " + genNumTwo + "?");
multiplicationData = Integer.parseInt(selectMul
inputOK = true; // code will never reach this spot if input is not correct
}
catch (NumberFormatException e)
{
}
}
if ( multiplicationData == (genNumOne * genNumTwo) )
{
correctAnswer() ;
result = result + 100;
}
else
{
incorrectAnswer() ;
}
}
public static void correctAnswer()
{
String[] arrGood = { "Very good!", "Well done", "Great!" };
Random randGood = new Random();
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDia
"Good Answer",
JOptionPane.INFORMATION_ME
}
public static void incorrectAnswer()
{
String[] arrBad = { "Sorry that was incorrect.", "Better luck next time", "Bad luck" };
Random randBad = new Random();
int new_rand_index = randBad.nextInt( arrBad.length );
String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDia
"Bad Answer",
JOptionPane.INFORMATION_ME
}
}
ASKER
Missed out userChoice=0;
Totally working now
Totally working now
ASKER
Also the alpha numeric checks don't work at all. Instead of looping the same input box it skips and goes to the next player.