1

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"); 
    }
  }
}

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Here's what I came up with, and now when you enter an incorrect integer (such as 5) it prompts again. I then enter the correct integer (such as 2) and it then I have to enter it again. Here's the code: pastebin.com/7y0DriKg
@JakePetersen This Code works.. Here is the complete code for you, i thought you can adapt it.Scanner scan = new Scanner(System.in); boolean a = true; do { System.out.println("Insert your Value:"); int value = scan.nextInt(); if (value == 1 ||value == 2 ||value == 3 ||value == 4 )a = false; } while (a);
@JakePetersen please note my answer as correct, than the topic would be closed, thanks
0

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; 
    }
}

1 Comment

Could the downvoter actually explain whats wrong with this answer
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.