Good day. My program below should give the user another chance to continue if there is an error and stop the process if there are no more errors, ie, the input is valid.
import java.util.*;
import javax.swing.*;
public class Format{
public static boolean formatter(String phoneNumber){
boolean thereIsAnError = true;
try {
String telephone = "";
if (phoneNumber.length() != 10)
throw new InputMismatchException();
long number = Long.parseLong(phoneNumber);
if (number < 0)
throw new InputMismatchException();
while (number > 0) {
telephone = telephone + " " + number%10;
number = number/10;
}
StringTokenizer numTokenizer = null;
numTokenizer = new StringTokenizer(telephone);
int numOfTokens = numTokenizer.countTokens();
StringBuilder formatted = new StringBuilder();
String[] numberArray = new String[10];
for (int i=0; i<numOfTokens; i++) {
numberArray[i] = numTokenizer.nextToken();
}
formatted.append("(");
if (numberArray[9] == null)
numberArray[9] = "0";
formatted.append(numberArray[9]);
if (numberArray[8] == null)
numberArray[8] = "0";
formatted.append(numberArray[8]);
if (numberArray[7] == null)
numberArray[7] = "0";
formatted.append(numberArray[7]);
if (numberArray[6] == null)
numberArray[6] = "0";
formatted.append(")");
formatted.append(" ");
formatted.append(numberArray[6]);
if (numberArray[5] == null)
numberArray[5] = "0";
formatted.append(numberArray[5]);
if (numberArray[4] == null)
numberArray[4] = "0";
formatted.append(numberArray[4]);
if (numberArray[3] == null)
numberArray[3] = "0";
formatted.append("-");
formatted.append(numberArray[3]);
if (numberArray[2] == null)
numberArray[2] = "0";
formatted.append(numberArray[2]);
if (numberArray[1] == null)
numberArray[1] = "0";
formatted.append(numberArray[1]);
if (numberArray[0] == null)
numberArray[0] = "0";
formatted.append(numberArray[0]);
formatted.toString();
JOptionPane.showMessageDialog(null, formatted);
}
catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null, "Please enter only digits. No other characters.", "This is fun." , JOptionPane.ERROR_MESSAGE);
}
catch (InputMismatchException e) {
JOptionPane.showMessageDialog(null, "Please enter 10 positive digits.", "This is fun." , JOptionPane.ERROR_MESSAGE);
}
return thereIsAnError;
}
public static void main(String[] args) {
String inputNumber = "This program receives a 10-digit phone number as input\nand produces a phone number formatted as (xxx) xxx-xxxx.\n\nInput a 10-digit phone number (just numbers, nothing else):";
boolean thereIsAnError = true;
while (thereIsAnError) {
try {
String phoneNumber = JOptionPane.showInputDialog(inputNumber);
phoneNumber = phoneNumber.trim();
thereIsAnError = formatter(phoneNumber);
}
catch (NullPointerException e){
JOptionPane.showMessageDialog(null, "Thank you. Goodbye.");
System.exit(0);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "There is something wrong. Shutting down.");
}
finally {
JOptionPane.showMessageDialog(null, "All exceptions avoided or handled!");
}
}
}
}
I think I am not placing the correct catch blocks. Please help me figure out what’s wrong. I’m new in Java and I’m still learning. Thank you!
You need to set
thereIsAnErrorto false when the correct input is provided so that the loop stops.Probably after…