473,799 Members | 3,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

10 New Member
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 23796
sukatoa
539 Contributor
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.prin tf...

Update us,
sukatoa
Mar 27 '08 #2
Navdip
22 New Member
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 New Member
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
12978
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 numbers in canonical format, e.g. +011 (408) 2578652, will fix the problem -- but I tried and it didn't work. I'm wondering if anyone can recommend an alternate phone dialer -- preferable one that does not use a GUI like dialer.exe, which...
2
1930
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 javascript function called on "onblur" event of the textbox. However, it seems validation happens before the onblur event, therefore if I enter "1234567890", the validator that checks the phone number is set to "Invalid" state. How can I format the...
4
4551
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
3368
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 successfully call a function that formats the phone number when the control is filled. But when updating, the value being passed in to my format function is vbNull instead of the value in the phone number field of the control. How can I get the update...
3
6791
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 accept any number of numbers
7
4653
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 digits). The only given is that there will be 10 digits in the string, but people may include spaces, parens, or other characters. These are all strings I'd expect $p = "5558675309";
5
3298
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 "special" because they are easily translated into words. You've been asked to create a list of phone numbers that are directly mappable to words by searching a dictionary for every 7 or 10 letter word that maps on to the phone lettering scheme: ...
0
3211
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 named "l" on the next page. This variable is the Last name reference to search a name on a corporate directory. here are the 2 pages of code first it's a keyboard like html clickable picture it refere coordinates to zone from the TouchArea tag...
6
4257
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 this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.