469,292 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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

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
3 22751
sukatoa
539 512MB
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
Navdip
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
Navdip
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.

Similar topics

2 posts views Thread by Oleg Ogurok | last post: by
4 posts views Thread by Brian Henry | last post: by
1 post views Thread by womblesjc | last post: by
7 posts views Thread by laredotornado | last post: by
5 posts views Thread by lim4801 | last post: by
terryble
reply views Thread by terryble | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.