Reputation: 25
I am trying to validate a user input, cuPerTerm > 12
I get the error message but the program continues and uses the invalid input to run
package gradplanner;
import java.util.Scanner;
public class GradPlanner {
int cuToComp;
int cuPerTerm;
public static void main(String[] args) {
final double COST = 2890.00; //flat-rate tuition rate charged per term
final int MONPERTERM = 6; //number of months per term
int cuToCompTotal = 0;
int numTerm;
int numMonToComp;
double tuition;
//prompt for user to input the number of CUs for each individual course remaining.
Scanner in = new Scanner(System.in);
System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");
int cuToComp = in.nextInt();
//add all CUs from individual courses to find the Total number of CUs left to complete.
while (cuToComp > 0)
{
cuToCompTotal += cuToComp;
System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");
cuToComp = in.nextInt();
}
System.out.println("The total number of CUs left is " + cuToCompTotal);
//prompt for user to input how many CUs they plan to take per term.
System.out.print("How many credit units do you intend to take per term? ");
int cuPerTerm = in.nextInt();
if (cuPerTerm <12) //validate input - Undergraduate Students Must enroll in a minimum of 12 CUs per term
{
System.out.print("Undergraduate Students must enroll in a Minimum of 12 CUs per Term. ");
}
//Calculate the number of terms remaining, if a remain is present increase number of terms by 1.
numTerm = cuToCompTotal/cuPerTerm;
if (cuToCompTotal%cuPerTerm > 0)
{
numTerm = numTerm + 1;
}
System.out.println("The Number of Terms you have left is " + numTerm + " Terms. ");
//Calculate the number of Months left to complete
numMonToComp = numTerm * MONPERTERM;
System.out.println("Which is " + numMonToComp + " Months. ");
//calculate the tuition cost based on the number of terms left to complete.
tuition = numTerm * COST;
System.out.println("Your Total Tuition Cost is: " + "$" + tuition +" . ");
}
}
I need it to continue to re-ask until 12 or something greater is entered. and then continue the program.
Upvotes: 0
Views: 2199
Reputation: 20889
There are pitfalls: Simple doing scanner.nextInt()
will give you the next Integer of the CURRENT Line.
If the user types in test
, nextInt()
will throw an InputMismatchException
, you have to handle. Also the int will NOT be consumed
So you have to call scanner.nextLine()
in between to Clean the current (mismatched) result.
All together something like this:
do{
try
{
System.out.print("Enter number > 12: ");
System.out.flush();
number = scanner.nextInt();
if (number > 12)
done = true;
}
catch(InputMismatchException e) {
System.out.println("This is not a number");
scanner.nextLine() //!Important!
}
}while(!done);
Upvotes: 1
Reputation: 1695
I think that a do-while
loop will best suit your needs:
int val;
do {
val = in.nextInt();
} while (val < 12);
Upvotes: 0
Reputation: 1926
Add this to continue getting input till it satisfies your condition:
while(cuPerTerm <= 12){
//Ask use to provide input
}
It is simple while loop which checks your input condition and continues taking input till it is satisfied.
Edit: - Initialize your cuPerTerm =0
while(cuPerTerm <= 12)
{
System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");
int cuToComp = in.nextInt();
}
Upvotes: 1
Reputation: 28752
Here's a simple solution:
int cuPerTerm = -1; // intialize to an invalid value
while (cuPerTerm < 12) {
System.out.print("How many credit units do you intend to take per term? ");
int cuPerTerm = in.nextInt();
if (cuPerTerm <12) { //validate input - Undergraduate Students Must enroll in a minimum of 12 CUs per term
System.out.print("Undergraduate Students must enroll in a Minimum of 12 CUs per Term. ");
}
}
Upvotes: 1
Reputation: 178263
You should use a while
loop so that you continue looping until cuPerTerm
is at least 12. Remember to take the user input again with cuPerTerm = in.nextInt();
inside the while
loop.
Upvotes: 1