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);