jd55
jd55

Reputation: 143

How to get the question to loop if the user input is incorrect

Whenever the user enters data that is incorrect, the program puts out a message telling the user to try again, but it then goes back to the main menu instead of letting the user try an input a different value. How can I do this?

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();
    if (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
    }
    else
    {
        System.out.println("Enter Module Mark: ");
        inputMark = in.nextInt();
        if (inputMark <0 || inputMark >100)
        {
            System.out.println("PLease enter a module mark between 0-100");
        }
        else
        {
            addStudent(inputStudentID, inputMark);
            System.out.println();
            System.out.println("New student record has been " + 
                        "successfully created");
            System.out.println();
            countRecords();
        }
    }
}

Upvotes: 1

Views: 63

Answers (3)

gx16
gx16

Reputation: 150

Change the if-else statement to while statement.

public static void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();

    while (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextInt();
    while (inputMark <0 || inputMark >100)
    {
        System.out.println("PLease enter a module mark between 0-100");
    }
    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been " + 
                "successfully created");
    System.out.println();
    countRecords();

}

Also, you can set a timeout variable to define the max number of times that user can try again. Such like,

public static void createStudentRecord(Scanner in)
{
    int timeout1 = 3, timeout2 = 3;

    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();

    while (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
        --timeout1;
        if( timeout1 == 0 ) {
            System.out.println("say something here");
            return;
        }
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextInt();
    while (inputMark <0 || inputMark >100)
    {
        System.out.println("PLease enter a module mark between 0-100");
        --timeout2;
        if( timeout2 == 0 ) {
            System.out.println("say something here");
            return;
        }
    }
    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been " + 
                "successfully created");
    System.out.println();
    countRecords();

}

Upvotes: 0

ninja.coder
ninja.coder

Reputation: 9648

One way to do it is by looping until you see the correct/desired input. One problem with this kind of approach is that it will never terminate if user doesn't enter the expected values (if this is what you are looking for),

Code Snippet:

public void createStudentRecord(Scanner in) {
    String inputStudentID = null;
    int inputMark = 0;

    while (true) {
        System.out.println("Enter Student ID: ");
        inputStudentID = in.nextLine();
        if (!(inputStudentID.length() == 6)) {
            System.out.println("Enter a student ID that is 6 characters");
        } else {
            break;
        }
    }

    while (true) {
        System.out.println("Enter Module Mark: ");
        inputMark = Integer.parseInt(in.nextLine());
        if (inputMark < 0 || inputMark > 100) {
            System.out.println("PLease enter a module mark between 0-100");
        } else {
            break;
        }
    }

    addStudent(inputStudentID, inputMark);
    System.out.println("\nNew student record has been successfully created.\n");
    countRecords();
}

Upvotes: 2

Brandon Dyer
Brandon Dyer

Reputation: 1386

There are two ways I would go about solving this problem. The first is to loop on each variable until a valid response is given.

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = -1;

    while (inputStudentID == null)
    {
      System.out.println("Enter Student ID: ");
      inputStudentID = in.nextLine();
      if (inputStudentID.length() != 6)
      {
          System.out.println("Enter a student ID that is 6 characters");
          inputStudentID = null;
      }
    }

    while (inputMark == -1)
    {
        System.out.println("Enter Module Mark: ");
        inputMark = in.nextLine();
        if (inputMark < 0 || inputMark > 100) {
            System.out.println("PLease enter a module mark between 0-100");
            inputMark = -1;
        }
    }

    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been successfully created");
    System.out.println();
    countRecords();
}

The second is to use recursion.

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    inputStudentID = in.nextLine();
    if (inputStudentID.length() == 6)
    {
        System.out.println("Enter a student ID that is 6 characters");
        createStudentRecord(in);
        return;
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextLine();
    if (inputMark < 0 || inputMark > 100)
    {
        System.out.println("PLease enter a module mark between 0-100");
        createStudentRecord(in);
        return;
    }

    addStudent(inputStudentID, inputMark);
    System.out.println("\nNew student record has been successfully created\n");
    countRecords();
}

Upvotes: 0

Related Questions