Home > Enterprise >  How can I use throw exception in this context?
How can I use throw exception in this context?

Time:12-01

So for this assignment, it asks the user to enter a phone number, then it splits the number up into a category of each set of integers. What I'm attempting to do is to throw a simple exception that if they do not enter the parenthesis for the area code that it throws the exception but doesn't crash the program and asks them to re-enter using the correct format

public class App{

public static void main(String[] args) throws Exception {
       Scanner input = new Scanner(System.in);
       
       String inputNum;
       String token1[];
       String token2[];
       String areaCode;
       String preFix;
       String lineNum;
       String fullNum;
       
       System.out.print("Enter a phone number in (123) 123-4567 format: ");
       
       inputNum = input.nextLine();
       System.out.println();

       
       token1 = inputNum.split(" ");
       areaCode = token1[0].substring(1, 4);
       if (token1[0].substring(0, 3) != "()"){
       throw new Exception("Enter a phone number in (123) 123-4567 format: ");
        }
       
       token2 = token1[1].split("-");
       
       preFix = token2[0];
       
       lineNum = token2[1];
       fullNum = "("   areaCode   ")"   " "   preFix   "-"   lineNum ;
       
       System.out.print("Area code: "   areaCode   "\n");
       System.out.print("Prefix: "   preFix   "\n");
       System.out.print("Line number: "   lineNum   "\n");
       System.out.print("Full number: "   fullNum);
    }
}

CodePudding user response:

No need to throw. Just keep asking in a loop.

String areaCode;
String preFix;
String lineNum;

while (true) {
    System.out.print("Enter a phone number in (123) 123-4567 format: ");
       
    String inputNum = input.nextLine();
    System.out.println();
   
    String [] token1 = inputNum.split(" ");
    if (token1.length == 2 && token1[0].length() == 5
            && token1[0].charAt(0) == '(' && token1[0].charAt(4) == ')') {
        areaCode = token1[0].substring(1, 4);
        String [] token2 = token1[1].split("-");
        if (token2.length == 2 && token2[0].length() == 3 && token2[1].length() == 4) {
            preFix = token2[0];
            lineNum = token2[1];
            // If we reach this line all is ok. Exit the loop.
            break;
        }
    }
}
String fullNum = "("   areaCode   ")"   " "   preFix   "-"   lineNum ;
System.out.print("Area code: "   areaCode   "\n");
System.out.print("Prefix: "   preFix   "\n");
System.out.print("Line number: "   lineNum   "\n");
System.out.print("Full number: "   fullNum);

CodePudding user response:

Try a try/catch or a try/catch/finally as your code will resume if and after your try-catch block catches the exception

try {
  //  Block of code to try
}
catch(Exception e) {
  //  Block of code to handle errors
}

////////////////////////

try {
 //  Block of code to try
} catch (Exception e) {
 //  Block of code to handle errors
} finally {
 //  Block of code to execute after try...catch, regardless of the result
    }
  •  Tags:  
  • java
  • Related