2

I'm asking for a phone number through input Scanner class and I'm enforcing numbers only but I might be going about this wrong. I need to have string of ints and not something that is one int and is bigger then the size capacity of an int. Ultimately, it needs to be 7 ints in length or 10 ints and not in between or more or less but of course all digits and not letters.

System.out.println("What is the  phone number (digits only)?: ");
 while (!Main.scan.hasNextInt()) 
            {
                Main.scan.next();
                System.out.println("What is the  phone number (digits only)?: ");
            }

            int phone_number = Main.scan.nextInt();

Clearfication string of ints - not meaning array of ints but series of independent ints but of course if you assign as one int, it can not tell the difference so you must use a long. This is what I'm referencing, the fact that my phone number is series of ints and not one int so primitive type is sort half right and and half wrong for me.

Solution: Thanks to Braj !!

    String numbers = Main.scan.next();
        long phone_number = 0 ;

         while (!numbers.matches("(\\d{7}|\\d{10})$"))
         {


             System.out.println("What is the patient's phone number (digits only)?: ");
             numbers = Main.scan.next();
         } 

         phone_number = Long.valueOf(numbers);
user3590149
  • 1,525
  • 7
  • 22
  • 25
  • what if the number entered was -9876543? Is `-` a valid phone "number" – phuclv Jul 17 '14 at 01:30
  • Not valid. Making it simple US format. Area Code and 7 digits or just 7 digits. – user3590149 Jul 17 '14 at 01:33
  • It's a string of *digits*, but storing each digit as a separate `int` (using 4 bytes to hold a value that can never be greater than 9) is silly. – Wyzard Jul 17 '14 at 01:33
  • "not meaning array of ints but series of independent ints" - what you're describing *is* an arrayed/aggregated data by the very definition. Using separate variables for vector data is insane IMO. –  Jul 17 '14 at 01:34
  • maybe the number is just a company directory operator and you need some more extension numbers to reach the real people – phuclv Jul 17 '14 at 01:39
  • @user3590149 see my another answer. – bumbumpaw Jul 17 '14 at 01:40

2 Answers2

5

A phone number is not an integer: 987-555-1212 does not represent the number nine billion, eight hundred seventy-five million, five hundred fifty-one thousand two hundred twelve. It's just a string of digits.

Phone numbers can be also be very long (many digits) when you get into things like extension numbers and special dialing codes, and they can even contain symbols like "#" and "*".

I'd store phone numbers as strings, with code to check that the user's input contains no invalid characters.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • I'd give you +1 if not for the fact I've already pointed this out to OP by stating "It depends on what you want to do with it later. Usually you'd be good enough with simply storing it as a String." –  Jul 17 '14 at 01:28
  • 1
    a phone number can also contains `+` and pause – phuclv Jul 17 '14 at 01:29
  • @Wyzard Would you parse the string and use a REGEX to validate the string? – user3590149 Jul 17 '14 at 01:48
  • what OP has stated in the post. OP is not masking the phone numbers just want to accept digits that is either 7 and 10 times – Braj Jul 17 '14 at 02:08
1

First 2147483647 is the maximum value of int hence to accommodate 10 digits numbers greater than this one, you need long.

You can accept a string and can validate the string using regex.

Read A comprehensive regex for phone number validation

sample code:

String regex = // get the pattern from the above link
String numbers = scanner.next();
if (numbers.matches(regex) {
    System.out.println("valid");
} else {
    System.out.println("Not valid");
}

Downvoter read the requirement of OP first before downvote

As per OP question

it needs to be 7 ints in length or 10 ints and not in between or more or less but of course all digits and not letters.

As per OP comment

I just want a way to validate a string that is all digits that is either 10 or 7 digits long.

Here is DEMO

sample code:

String numbers = scanner.next();
if (numbers.matches("(\\d{7}|\\d{10})$")) {
    int mobile = Integer.valueOf(numbers);
    System.out.println("valid");
} else {
    System.out.println("Not valid");
}
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76