Write a program to split number into digits in java


Asked by admin @ in Computers and Technology viewed by 335 People


Write a Java program that asks the user to enter a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit "exchange," and the remaining 4-digit number as separate strings, prints them, and then prints the complete telephone number in the usual formatting with parentheses. If the user does not enter a 10-digit number, print an error message.

Answered by admin @



Answer:

import java.util.Scanner; // package import

public class Main // main class

{

   public void phone() //function definition

   {

      Scanner ob = new Scanner(System.in);// scanner class

      String phno = " ";

     System.out.println("Enter 10-digit of Us telephone number: ");

      phno = ob.next();

      if(phno.length() != 10) // checking the length of phone number

      {

          System.out.println("Enter a valid 10-digit number..."); // print error message

      }

      else

      {

          System.out.println("The phone number you entered: "+phno);

          String a = phno .substring(0,3); // calculate the area code

          String exh = phno.substring(3,6);//calculate the exchange code

          String num1 = phno.substring(6,10);//calculate the phone number

          System.out.println("Area code is :"+a);// display area code

          System.out.println("Exchange is: "+exh);// display exchange code

          System.out.println("Number is: "+num1);//display  phone number

          System.out.println("The complete telephone number : ("+a+") "+exh+"-"+num1);// DISPLAY COMPPLETE NUMBER

      }

   }

public static void main(String[] args) // MAIN METHOD

{

Main ob =new Main(); // CREATING an object of class Main

ob.phone();// calling function phone method

}

}

Explanation:

In the given program we create a function phone of void return type  and input a phone number in "phno " variable by using scanner class object After that check the length of phone number by using length() method if length is 10 then we extracts the area code ,exchange code and  remaining number by using substring method which is used to extract the character from string and finally display the complete telephone number in proper formatting. If length does not equal to 10 it print error message .

Output:

Enter 10-digit of Us telephone number:1234567890

The phone number you entered:1234567890

Area code is :123

Exchange is:456

Number is:7890

The complete telephone number : (123) 456-7890


Similar Questions

Write a program to find average of three numbers

Asked by admin @ in Computer Science viewed by 336 persons

Write a program to find average of three numbers.

Write a 3 digit number using digits 2 9 4

Asked by maham237 @ in Mathematics viewed by 665 persons

Write a 3- digit number using digits 2,9,4 draw a quick picture to show the value of your number

Write the successor of the greatest 5 digit number

Asked by admin @ in Math viewed by 304 persons

Write the predecessor and the successor of greatest 5 digit number

How to write 10 digit number in indian system

Asked by admin @ in Math viewed by 302 persons

I want a 10 digit number name in indian system I want number name​

Write the predecessor of the smallest 6 digit number

Asked by admin @ in Math viewed by 302 persons

6. Write the predecessor of the smallest 6-digit number.

Write a program to print a simple message in java

Asked by admin @ in Computer Science viewed by 345 persons

Write a program to print a simple message in java​

Write the greatest 4 digit number using three different digits

Asked by admin @ in Math viewed by 325 persons

Greatest 4 -digit number using 3 different digits=​

Write a program to swap two numbers in python

Asked by admin @ in Computer Science viewed by 344 persons

Write a program is python to swap two numbers.

Write a program to calculate area of circle in c++

Asked by admin @ in Computers and Technology viewed by 311 persons

I want to write a C++ program to read the diameter of a circle. The program should find and display the area and circumference of the circle. Define a constant …

How do you write a mixed number into a decimal

Asked by admin @ in Mathematics viewed by 339 persons

What is the mixed number 4 4/25 as a decimal

How to write a mixed number into an improper fraction

Asked by admin @ in Mathematics viewed by 293 persons

How do you convert mixed numbers into improper fractions and vice versa?

C program to count number of words in a file

Asked by admin @ in Computers and Technology viewed by 348 persons

Write a c program to count the total number of commented characters and words in a c file taking both types of c file comments (single line and block) into …

How to write a improper fraction to a mixed number

Asked by admin @ in Mathematics viewed by 343 persons

Three of the following steps are used in writing a mixed number as an improper fraction. Which step does NOT belong? Multiply the denominator of the fractional part by the …

How do you write a fraction as a mixed number

Asked by admin @ in Mathematics viewed by 275 persons

What is the position of E on the number line below? write your answer as a fraction or mixed number,

The sum of two digit number is 9

Asked by admin @ in Math viewed by 337 persons

The sum of digits of a two-digit number is 9.Also,nine times this number is twice the number obtained by reversing the order of the digits.Find the number.

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4432 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 609 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 594 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 576 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 574 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 563 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 537 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 514 persons