我需要编写一段代码来计算用户输入的数量。如果用户输入的数字多于典型电话号码(例如,输入9589889898989
),则程序必须显示错误并要求用户重新输入该号码。如果用户输入了正确的数字(例如,5083588745
),程序应以正确的格式打印该数字。我怎么能这样做?
注意:除了可以读取各个数字的代码之外,一切都已完成。
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter your 10 digit #.");
long nrmNum = 1111111111;
long phoneFmt = 0l;
phoneFmt = (long) input.nextDouble();
//get a 12 digits String, filling with left '0′ (on the prefix)
java.text.DecimalFormat phoneDecimalFmt =
new java.text.DecimalFormat("0000000000");
String phoneRawString = phoneDecimalFmt.format(phoneFmt);
java.text.MessageFormat phoneMsgFmt =
new java.text.MessageFormat("({0}) - {1} - {2}");
//suposing a grouping of 3-3-4
String[] phoneNumArr = {
phoneRawString.substring(0, 3),
phoneRawString.substring(3, 6),
phoneRawString.substring(6)
};
nrmNum /= 1;
if (phoneFmt == nrmNum) {
System.out.println("Your Phone number is:");
System.out.println(phoneMsgFmt.format(phoneNumArr));
} else {
System.out.print("Please input 10 numbers for the phone#");
System.out.println(phoneMsgFmt.format(phoneNumArr));
}
}