Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m having a little problem, I have a big project which is to build a ca

ID: 3765243 • Letter: H

Question

Hello, I'm having a little problem, I have a big project which is to build a cab appointment app, I have built it, but I have one small issue, in the test file, I have to make sure the program doesn't crash if letters are entered in the phone number of the customer. But my loop isn't working, it is either throwing the catching message and crashing, or spamming the screen with the catching message. The program has to show the user the catching message if non numerics are entered and let him try again until he enters an 11 digit phone number. Help please :|

Here is a part of my test class:

Explanation / Answer

ANSWER:

Replace the WHILE LOOP in the code with WHILE (true) and check the condition in the try block .if condition satisfies then ask the input again.

public static void main(String[] args) throws FileNotFoundException, IOException, InputMismatchException   

{

       Scanner input = new Scanner(System.in);

       System.out.print("Please enter your e-mail address: ");

       String emailA = input.nextLine();

       System.out.print("Please enter your first name: ");

       String namE = input.nextLine();

       long phoneN = 0;

//GET INPUT UNTIL USER ENTER 11 DIGIT PHONE NUMBER

       while(true)

      {

        try

         {

              System.out.print("Please enter your 11-digit phone number: ");

                phoneN = input.nextLong();

//CHECK THE VALIDITY OF THE INPUT

                if(phoneN > 99999999999l || phoneN < 1000000000l)

                    continue;

//BREAK IF USER ENTERS CORRECT INPUT

break;

          }

//CATCH THE WRONG INPUTS

        catch (InputMismatchException e)

{

              System.out.print("Invalid input, please enter your 11-digit phone number: ");

          }

     }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote