By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

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

P: 10
I really need help with this. Your tips are greatly appreciated:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner;
  2.  
  3. public class PhoneNumbers
  4. {
  5.  public static void main(String[] args){
  6.     Scanner input;
  7.  
  8.        Scanner scan = new Scanner(System.in);//get user input
  9.  
  10.        System.out.print( "Please enter a phone number:");
  11.  
  12.        String word = scan.nextLine();
  13.     int numOfDigits = word.length();
  14.     boolean hasMoreDigits = false;
  15.     boolean hasLessDigits = false;
  16.  
  17.     if(numOfDigits > 5){
  18.       for (int i = 0; i < numOfDigits; ++i){
  19.         if (numOfDigits < 10){
  20.           hasLessDigits = true;
  21.         }
  22.         else if (numOfDigits > 10){
  23.           hasMoreDigits = true;
  24.         }
  25.  
  26.         if (hasLessDigits && hasMoreDigits){
  27.           break; // check finished
  28.         }
  29.       }
  30.  
  31.       if (hasMoreDigits){
  32.         System.out.println
  33.           ("Your phone number has more than 10 digits!");
  34.       }
  35.       else if (hasLessDigits){
  36.         System.out.println("Your phone number has less than 10 digits!");
  37.       }
  38.       else{
  39.         System.out.println("Your phone number is 10 digits!");
  40.  
  41.     }
  42.   }
  43. }
  44.  public static String reFormat (String word)
  45.  {
  46.    word = String.format("({0}) {1}-{2}",
  47.     word.Substring(0, 3),
  48.     word.Substring(3, 3),
  49.     word.Substring(6));
  50.  
  51.  }}
Mar 27 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 539
I really need help with this. Your tips are greatly appreciated:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner;
  2.  
  3. public class PhoneNumbers
  4. {
  5.  public static void main(String[] args){
  6.     Scanner input;
  7.  
  8.        Scanner scan = new Scanner(System.in);//get user input
  9.  
  10.        System.out.print( "Please enter a phone number:");
  11.  
  12.        String word = scan.nextLine();
  13.     int numOfDigits = word.length();
  14.     boolean hasMoreDigits = false;
  15.     boolean hasLessDigits = false;
  16.  
  17.     if(numOfDigits > 5){
  18.       for (int i = 0; i < numOfDigits; ++i){
  19.         if (numOfDigits < 10){
  20.           hasLessDigits = true;
  21.         }
  22.         else if (numOfDigits > 10){
  23.           hasMoreDigits = true;
  24.         }
  25.  
  26.         if (hasLessDigits && hasMoreDigits){
  27.           break; // check finished
  28.         }
  29.       }
  30.  
  31.       if (hasMoreDigits){
  32.         System.out.println
  33.           ("Your phone number has more than 10 digits!");
  34.       }
  35.       else if (hasLessDigits){
  36.         System.out.println("Your phone number has less than 10 digits!");
  37.       }
  38.       else{
  39.         System.out.println("Your phone number is 10 digits!");
  40.  
  41.     }
  42.   }
  43. }
  44.  public static String reFormat (String word)
  45.  {
  46.    word = String.format("({0}) {1}-{2}",
  47.     word.Substring(0, 3),
  48.     word.Substring(3, 3),
  49.     word.Substring(6));
  50.  
  51.  }}

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

Expand|Select|Wrap|Line Numbers
  1. 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.printf...

Update us,
sukatoa
Mar 27 '08 #2

P: 22
I really need help with this. Your tips are greatly appreciated:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner;
  2.  
  3. public class PhoneNumbers
  4. {
  5.  public static void main(String[] args){
  6.     Scanner input;
  7.  
  8.        Scanner scan = new Scanner(System.in);//get user input
  9.  
  10.        System.out.print( "Please enter a phone number:");
  11.  
  12.        String word = scan.nextLine();
  13.     int numOfDigits = word.length();
  14.     boolean hasMoreDigits = false;
  15.     boolean hasLessDigits = false;
  16.  
  17.     if(numOfDigits > 5){
  18.       for (int i = 0; i < numOfDigits; ++i){
  19.         if (numOfDigits < 10){
  20.           hasLessDigits = true;
  21.         }
  22.         else if (numOfDigits > 10){
  23.           hasMoreDigits = true;
  24.         }
  25.  
  26.         if (hasLessDigits && hasMoreDigits){
  27.           break; // check finished
  28.         }
  29.       }
  30.  
  31.       if (hasMoreDigits){
  32.         System.out.println
  33.           ("Your phone number has more than 10 digits!");
  34.       }
  35.       else if (hasLessDigits){
  36.         System.out.println("Your phone number has less than 10 digits!");
  37.       }
  38.       else{
  39.         System.out.println("Your phone number is 10 digits!");
  40.  
  41.     }
  42.   }
  43. }
  44.  public static String reFormat (String word)
  45.  {
  46.    word = String.format("({0}) {1}-{2}",
  47.     word.Substring(0, 3),
  48.     word.Substring(3, 3),
  49.     word.Substring(6));
  50.  
  51.  }}


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)
Mar 27 '08 #3

P: 22
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
Mar 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.