How to reformat a phone number (string) to (###) ###-####?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccarter45
    New Member
    • Mar 2008
    • 10

    How to reformat a phone number (string) to (###) ###-####?

    I really need help with this. Your tips are greatly appreciated:
    Code:
     import java.util.Scanner;
    
    public class PhoneNumbers
    {
     public static void main(String[] args){
        Scanner input;
    
           Scanner scan = new Scanner(System.in);//get user input
       
           System.out.print( "Please enter a phone number:");
        
           String word = scan.nextLine();
        int numOfDigits = word.length();
        boolean hasMoreDigits = false;
        boolean hasLessDigits = false;
    
        if(numOfDigits > 5){
          for (int i = 0; i < numOfDigits; ++i){
            if (numOfDigits < 10){
              hasLessDigits = true;
            }
            else if (numOfDigits > 10){
              hasMoreDigits = true;
            }
    
            if (hasLessDigits && hasMoreDigits){
              break; // check finished
            }
          }
    
          if (hasMoreDigits){
            System.out.println
              ("Your phone number has more than 10 digits!");
          }
          else if (hasLessDigits){
            System.out.println("Your phone number has less than 10 digits!");
          }
          else{
            System.out.println("Your phone number is 10 digits!");
       
        }
      }
    }
     public static String reFormat (String word)
     {
       word = String.format("({0}) {1}-{2}",
        word.Substring(0, 3),
        word.Substring(3, 3),
        word.Substring(6));
    
     }}
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by ccarter45
    I really need help with this. Your tips are greatly appreciated:
    Code:
     import java.util.Scanner;
    
    public class PhoneNumbers
    {
     public static void main(String[] args){
        Scanner input;
    
           Scanner scan = new Scanner(System.in);//get user input
       
           System.out.print( "Please enter a phone number:");
        
           String word = scan.nextLine();
        int numOfDigits = word.length();
        boolean hasMoreDigits = false;
        boolean hasLessDigits = false;
    
        if(numOfDigits > 5){
          for (int i = 0; i < numOfDigits; ++i){
            if (numOfDigits < 10){
              hasLessDigits = true;
            }
            else if (numOfDigits > 10){
              hasMoreDigits = true;
            }
    
            if (hasLessDigits && hasMoreDigits){
              break; // check finished
            }
          }
    
          if (hasMoreDigits){
            System.out.println
              ("Your phone number has more than 10 digits!");
          }
          else if (hasLessDigits){
            System.out.println("Your phone number has less than 10 digits!");
          }
          else{
            System.out.println("Your phone number is 10 digits!");
       
        }
      }
    }
     public static String reFormat (String word)
     {
       word = String.format("({0}) {1}-{2}",
        word.Substring(0, 3),
        word.Substring(3, 3),
        word.Substring(6));
    
     }}

    Try to have some experiments on the code below...

    Code:
    System.out.printf("%s %d %.2f %.5f %c", "I love java",10, 1.2034, 1.1, 'z');
    And try it to use String.format() instead of using System.out.prin tf...

    Update us,
    sukatoa

    Comment

    • Navdip
      New Member
      • Mar 2008
      • 22

      #3
      Originally posted by ccarter45
      I really need help with this. Your tips are greatly appreciated:
      Code:
       import java.util.Scanner;
      
      public class PhoneNumbers
      {
       public static void main(String[] args){
          Scanner input;
      
             Scanner scan = new Scanner(System.in);//get user input
         
             System.out.print( "Please enter a phone number:");
          
             String word = scan.nextLine();
          int numOfDigits = word.length();
          boolean hasMoreDigits = false;
          boolean hasLessDigits = false;
      
          if(numOfDigits > 5){
            for (int i = 0; i < numOfDigits; ++i){
              if (numOfDigits < 10){
                hasLessDigits = true;
              }
              else if (numOfDigits > 10){
                hasMoreDigits = true;
              }
      
              if (hasLessDigits && hasMoreDigits){
                break; // check finished
              }
            }
      
            if (hasMoreDigits){
              System.out.println
                ("Your phone number has more than 10 digits!");
            }
            else if (hasLessDigits){
              System.out.println("Your phone number has less than 10 digits!");
            }
            else{
              System.out.println("Your phone number is 10 digits!");
         
          }
        }
      }
       public static String reFormat (String word)
       {
         word = String.format("({0}) {1}-{2}",
          word.Substring(0, 3),
          word.Substring(3, 3),
          word.Substring(6));
      
       }}


      HI...you can try the following code format to solve your problem.....

      -------------------------------------------------------------------------------------------------
      Again: please don't feed boiler plate solution code; the OP won't learn anything
      from copying/pasing code from somebody else. Read the forum guidelines about
      this very matter (see the 'Help' link near the top right of this page). If you continue
      posting code like this you risk a ban from this forum site. Don't do it anymore.

      kind regards,

      Jos (moderator)

      Comment

      • Navdip
        New Member
        • Mar 2008
        • 22

        #4
        Originally posted by Navdip
        HI...you can try the following code format to solve your problem.....

        -------------------------------------------------------------------------------------------------
        Again: please don't feed boiler plate solution code; the OP won't learn anything
        from copying/pasing code from somebody else. Read the forum guidelines about
        this very matter (see the 'Help' link near the top right of this page). If you continue
        posting code like this you risk a ban from this forum site. Don't do it anymore.

        kind regards,

        Jos (moderator)
        hi...i just wanted to describe how code will be optimize...i didn't do spoon feeding...i was trying to describe what should be our approach to sovle the problem...the person who has raised the request was doing many extra things in his...so i wrote that code....after all if u seem i have made a wrong thing then sorry for that.....thaks

        Comment

        Working...