Skip to content
Advertisement

Phone number breakdown (International Version) java

Given a long integer representing a 11-digit phone number, output the country code, area code, prefix, and line number using the format +1 (800) 555-1212.

Ex: If the input is: 18005551212

the output is:

+1 (800) 555-1212

So far,

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) {

      Scanner scnr = new Scanner(System.in);
      long phoneNumber;
      int countryCode, area_Code, prefix, line_Number;

      phoneNumber = scnr.nextLong();

      /* Type your code here. */
      line_Number = (int)(phoneNumber%10000);
      countryCode = (int)(phoneNumber%100/10);
      area_Code = (int)(phoneNumber/10000/1000);



      System.out.println("(" + area_Code + ") ");
}

I am running program just to see how I want to go about for the rest.

I have a question. After running, how can I remove the 1 in and for the area_Code?

It appears like this (1800), I want to place the 1 outside the parentheses.

Any help please, kind of newbie still.

Advertisement

Answer

to output 800 you can use : area_Code % 1000 and to output the 1 you can use : area_Code / 1000

System.out.println(area_Code / 1000 + "(" + area_Code % 1000 + ") ");
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement