473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 23757
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

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

Similar topics

2
by: deko | last post by:
The below code dials a phone number using C:\Program Files\Windows NT\dialer.exe. The problem is that dialing rules are ignored. MS Knowledge Base Article 266253 suggests that entering the phone...
2
by: Oleg Ogurok | last post by:
Hi there, I have a modified ASP.NET TextBox control that formats its output as the phone number, e.g. if you enter "1234567890" it'll change the value to "123-456-7890". This is done by a...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
3
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
7
by: laredotornado | last post by:
Hi, Using php 4.4.4, I am looking to parse a US phone number string into 3 smaller strings -- area code, the first part of the phone number (3 digits) and the last part of the phone number (4...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
0
terryble
by: terryble | last post by:
hy, We are deploying IPPhone cisco(R) On this phone proprietary code exist with XML too. After code modification, a can't find the final step for this service to work. I need to post the variable...
6
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.