Reputation:
I am currently writing a program (java) that asks users for their class schedule, and how many credits each class is. The only valid answers for credits should be 1, 2, 3, or 4. How would I check to make sure these are the only valid ones? If they input an invalid amount, I want it to loop and prompt them for the exact same question. Here is what I have so far.
//Jake Petersen
import java.util.Scanner;
public class test{
//I promise not to make all methods static in CS1
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.println("Please enter a course:");
courseArray[i] = scan.nextLine();
}
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++){
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
creditArray[i] = scan.nextInt();
}
int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}
Upvotes: 1
Views: 116
Reputation: 1651
You could do it with a while loop and a boolean-variable to get the state checked
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++) {
boolean isValid = true;
while(isValid)
{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
isValid = false;
creditArray[i] = buffer;
}
}
A second way would be to use a do-while-statement something like this:
boolean isValid = true;
do{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
a=false;
}
}while(a);
Upvotes: 0
Reputation: 7347
As an alternative you could simply pull the incrementation out of the loop and just increment i
if the input is valid, like this:
for (int i = 0; i < creditArray.length;) {
System.out.println("Please enter how many credits "+ courseArray[i] + " is:");
int input = scan.nextInt();
if (input >= 1 && input <= 4) {
creditArray[i++] = input;
}
}
Upvotes: 0
Reputation: 1094
you have to do it in this way.
Scanner scan = new Scanner(System.in);
boolean a = true;
do{
System.out.println("Insert your Value:");
int value = scan.nextInt();
if(value==1||......)a=false;
}while(a);
This Section you can insert into your code. Than it will Ask again
Upvotes: 4