Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:48:56+00:00 2026-06-09T05:48:56+00:00

Good day. My program below should give the user another chance to continue if

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:48:57+00:00Added an answer on June 9, 2026 at 5:48 am

    You need to set thereIsAnError to false when the correct input is provided so that the loop stops.

    Probably after…

     formatted.toString();
     JOptionPane.showMessageDialog(null, formatted);
     thereIsAnError = false;  // <--- No error should be reflected and returned.
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I encountered the following error upon running my JSP program. java.lang.IllegalStateException: PWC3991:
Good day, I try to start new Activity from another. But it always crashed.
Good day, My code generates a run-time error and I'm not sure how to
Good day! I am modifying some codes from an existing program. Before the program
Good day, stackoverflow, I need to suggest to user different contexts for a word,
Good day! Is there any way to include a timer (timestamp?or whatever term it
good day sirs. i have a small program that reads and processes data via
good day... i am creating an address book program... i used a switch in
good day.. i have an Address Book Program... it runs properly but it doesn't
good day... i have a problem in searching my entries in my addressbook program...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.