import java.util.Scanner;
public class phonenumberformat {
public static void main(String [] args) {
//Create the scanner class for user input
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your phone number: ");
String number = keyboard.next();
/* Separate the number into the three categories I want
* Side-Note: Since we can assume that the phone number will be 10 digits long I don't
* have to figure out the length of the number that the user will input
*/
String area_code = number.substring(0,3);
String first_three = number.substring(3,6);
String rest = number.substring(6,10);
int new_number = Integer.parseInt(number);
if (new_number < 10){
System.out.println("It seems that you have entered an invalid phone number, please try again!");
/* I want to make a loop here so that if the user accidentally enters more of less than
* a 10 digit number it will loop the "Please enter your phone number: " part so
* Java won't give an error
*/
} else {
//Concatenate the number properly so that it is formatted nicely
String formatted_number = "(" + area_code + ")" + " " + first_three + "-" + rest;
}
String final_number = Integer.toString(formatted_number);
//Print the newly formatted number
System.out.println(formatted_number);
}
}
所以这个程序应该格式化电话号码从1112223333到(111)222-3333。作业说我们可以假设用户将输入一个未格式化的电话号码,但是我想稍微安全一点,这样如果用户通过改变输入的数字不是10位数而不是错误它将提示他们重新输入号码。 谢谢!
答案 0 :(得分:0)
使用正则表达式:
String formatted = input.replaceAll("^(\\d{3})(\\d{3})(\\d{4})$", "($1) $2-$3");
这不仅是一个单线解决方案,它不会格式化已经格式化的数字(特别是如果输入不是11位数,则不会发生变化)。
如果您想强制格式替换已存在的格式,那么仍只有一行,只需删除所有非数字:
String formatted = input.replaceAll("\\D", "").replaceAll("^(\\d{3})(\\d{3})(\\d{4})$", "($1) $2-$3");
答案 1 :(得分:0)
我已经纠正了你的代码,其中有很多错误 你已经应用了Integer.toString(“string Variable”)你可以直接将String赋给String变量
// String rest = number.substring(6,10);它可以给你StringOutOfBoundException
试试这个
public static void main(String[] args) {
//Create the scanner class for user input
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your phone number: ");
String number = keyboard.next();
/* Separate the number into the three categories I want
* Side-Note: Since we can assume that the phone number will be 10 digits long I don't
* have to figure out the length of the number that the user will input
*/
String area_code = number.substring(0,3);
String first_three = number.substring(3,6);
String rest = number.substring(6,9);
String formatted_number ="";
/* I want to make a loop here so that if the user accidentally enters more of less than
* a 10 digit number it will loop the "Please enter your phone number: " part so
* Java won't give an error
*/
int new_number = Integer.parseInt(number);
if (new_number < 10){
System.out.println("It seems that you have entered an invalid phone number, please try again!");
} else {
//Concatenate the number properly so that it is formatted nicely
formatted_number = "(" + area_code + ")" + " " + first_three + "-" + rest;
}
String final_number = formatted_number;
//Print the newly formatted number
System.out.println(formatted_number);
}
并且如果您希望用户重新输入该号码以防他出错,那么您必须使用Pattern类来检查它是否有效
答案 2 :(得分:0)
这应该有效:
import java.util.Scanner;
public class phonenumberformat {
public static void main(String [] args) {
//Create the scanner class for user input
Scanner keyboard = new Scanner(System.in);
boolean notValid = true;
System.out.println("Please enter your phone number: ");
while(notValid)
{
String number = keyboard.next();
if (number.length() !=10){
System.out.println("It seems that you have entered an invalid phone number, please try again!");
} else {
String area_code = number.substring(0,3);
String first_three = number.substring(3,6);
String rest = number.substring(6,10);
//Concatenate the number properly so that it is formatted nicely
String formatted_number = "(" + area_code + ")" + " " + first_three + "-" + rest;
//Print the newly formatted number
System.out.println(formatted_number);
notValid = false;
}
}
}
}