英文:
Re-prompt until correct data format input
问题
import java.util.Scanner;
public class PhoneNumberConverter {
public static boolean isValidPhoneNumber(String phoneNumber) {
if (phoneNumber.matches("\\d{4}\\w{7}"))
return true;
else if (phoneNumber.matches("\\d{1}\\d{3}\\w{7}"))
return true;
else
return false;
}
public static int translatePhoneNumber(String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = 'A';
int currentIndex;
if (firstIndex == 0 || firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
} else {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) >= 'A' && phoneNumber.charAt(currentIndex) <= 'Z') {
chartoNumber = (phoneNumber.charAt(currentIndex) - 'A') / 3 + 2;
} else
chartoNumber = phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);
}
}
return chartoNumber;
}
public static void main(String[] args) {
System.out.println("Enter a phone number to convert:");
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
String phoneNumber = enteredNumber.toUpperCase();
boolean validationResult = isValidPhoneNumber(phoneNumber);
System.out.println(validationResult);
input.close();
translatePhoneNumber(phoneNumber, 0, 1);
System.out.print("-");
translatePhoneNumber(phoneNumber, 1, 4);
System.out.print("-");
translatePhoneNumber(phoneNumber, 4, 7);
System.out.print("-");
translatePhoneNumber(phoneNumber, 7, 11);
}
}
英文:
I wrote this code for my Java course project that gets the phone number in the 1800LETTERS and outputs the number as 1-800-538-8377. The thing that I am struggling with is that I want to keep prompting until the user inputs the phone number in the correct format (1800LETTERS).
I know that I should use a while loop but I don't know where to plug it. Also the program should not run until the user inputs the correct format, otherwise, the code throws up some exceptions. I think if I use the while loop in correct place, the user should not be able to enter incorrect input.
I appreciate your help.
Here is my code:
import java.util.Scanner;
public class PhoneNumberConverter {
public static boolean isValidPhoneNumber(String phoneNumber) {
if (phoneNumber.matches("\\d{4}\\w{7}"))
return true;
else if (phoneNumber.matches("\\d{1}\\d{3}\\w{7}"))
return true;
else
return false;
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = 'A';
int currentIndex;
if (firstIndex == 0) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else if (firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == 'A' || phoneNumber.charAt(currentIndex) == 'B' || phoneNumber.charAt(currentIndex) == 'C' )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == 'D' || phoneNumber.charAt(currentIndex) == 'E' || phoneNumber.charAt(currentIndex) == 'F' )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == 'G' || phoneNumber.charAt(currentIndex) == 'H' || phoneNumber.charAt(currentIndex) == 'I' )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == 'J' || phoneNumber.charAt(currentIndex) == 'K' || phoneNumber.charAt(currentIndex) == 'L' )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == 'M' || phoneNumber.charAt(currentIndex) == 'N' || phoneNumber.charAt(currentIndex) == 'O' )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == 'P' || phoneNumber.charAt(currentIndex) == 'Q' || phoneNumber.charAt(currentIndex) == 'R' || phoneNumber.charAt(currentIndex) == 'S' )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == 'T' || phoneNumber.charAt(currentIndex) == 'U' || phoneNumber.charAt(currentIndex) == 'V' )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == 'W' || phoneNumber.charAt(currentIndex) == 'X' || phoneNumber.charAt(currentIndex) == 'Y' || phoneNumber.charAt(currentIndex) == 'Z' )
chartoNumber = 9;
else
chartoNumber = phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);
}
}
return chartoNumber;
}
public static void main(String[] args) {
System.out.println("Enter a phone number to convert:");
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
String phoneNumber = enteredNumber.toUpperCase();
boolean validationResult = isValidPhoneNumber(phoneNumber);
System.out.println(validationResult);
input.close();
translatePhoneNumber (phoneNumber, 0, 1);
System.out.print("-");
translatePhoneNumber (phoneNumber, 1, 4);
System.out.print("-");
translatePhoneNumber (phoneNumber, 4, 7);
System.out.print("-");
translatePhoneNumber (phoneNumber, 7, 11);
}
}
答案1
得分: 1
你在这里不一定需要使用 while 循环。
你可以创建一个函数来获取输入,验证输入并在有效时返回它。如果输入无效,抛出异常,在异常处理中再次调用该函数。
public String getInput() {
try {
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
if (!isValidPhoneNumber(enteredNumber)) {
throw new InputMismatchException("Wrong Format");
} else {
return enteredNumber;
}
} catch (InputMismatchException ex) {
return getInput();
}
}
然后在原来的 String enteredNumber = input.next();
位置调用 getInput
函数。
所以
String enteredNumber = getInput();
如果用户提供了错误的输入,该函数将被调用,直到提供正确的输入为止。
英文:
You don't necessarily need to use a while loop here.
You can create a function to get the input, validate it and return it if's valid. If it's not valid, throw an exception and in the exception handling, invoke the function again.
public string getInput() {
try{
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
if(!isValidPhoneNumber(enteredNumber)
throw new InputMismatchException("Wrong Format");
else
return enteredNumber;
} catch (InputMismatchException ex){
return getInput();
}
}
Then call getInput where your original String enteredNumber = input.next();
was
so
String enteredNumber = getInput()
If the user gets the wrong input, the function will be called until the right input is provided.
答案2
得分: 0
import java.util.Scanner;
public class PhoneNumberConverter {
public static boolean isValidPhoneNumber(String phoneNumber) {
if (phoneNumber.matches("\\d{4}\\w{7}"))
return true;
else if (phoneNumber.matches("\\d{1}\\d{3}\\w{7}"))
return true;
else
return false;
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = 'A';
int currentIndex;
if (firstIndex == 0) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else if (firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == 'A' || phoneNumber.charAt(currentIndex) == 'B' || phoneNumber.charAt(currentIndex) == 'C' )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == 'D' || phoneNumber.charAt(currentIndex) == 'E' || phoneNumber.charAt(currentIndex) == 'F' )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == 'G' || phoneNumber.charAt(currentIndex) == 'H' || phoneNumber.charAt(currentIndex) == 'I' )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == 'J' || phoneNumber.charAt(currentIndex) == 'K' || phoneNumber.charAt(currentIndex) == 'L' )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == 'M' || phoneNumber.charAt(currentIndex) == 'N' || phoneNumber.charAt(currentIndex) == 'O' )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == 'P' || phoneNumber.charAt(currentIndex) == 'Q' || phoneNumber.charAt(currentIndex) == 'R' || phoneNumber.charAt(currentIndex) == 'S' )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == 'T' || phoneNumber.charAt(currentIndex) == 'U' || phoneNumber.charAt(currentIndex) == 'V' )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == 'W' || phoneNumber.charAt(currentIndex) == 'X' || phoneNumber.charAt(currentIndex) == 'Y' || phoneNumber.charAt(currentIndex) == 'Z' )
chartoNumber = 9;
else
chartoNumber = phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);
}
}
return chartoNumber;
}
public static void main(String[] args) {
boolean validationResult=false;
do {
try{
System.out.println("Enter a phone number to convert:");
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
String phoneNumber = enteredNumber.toUpperCase();
validationResult = isValidPhoneNumber(phoneNumber);
if(validationResult==false){
System.out.println("Entered phone number is incorrect.");
}
System.out.println(validationResult);
input.close();
translatePhoneNumber(phoneNumber, 0, 1);
System.out.print("-");
translatePhoneNumber(phoneNumber, 1, 4);
System.out.print("-");
translatePhoneNumber(phoneNumber, 4, 7);
System.out.print("-");
translatePhoneNumber(phoneNumber, 7, 11);
System.out.println();
}
catch(Exception e){
e.printStackTrace();
}
}while(validationResult==false);
}
}
英文:
import java.util.Scanner;
public class PhoneNumberConverter {
public static boolean isValidPhoneNumber(String phoneNumber) {
if (phoneNumber.matches("\\d{4}\\w{7}"))
return true;
else if (phoneNumber.matches("\\d{1}\\d{3}\\w{7}"))
return true;
else
return false;
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = 'A';
int currentIndex;
if (firstIndex == 0) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else if (firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
chartoNumber = phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else {
for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == 'A' || phoneNumber.charAt(currentIndex) == 'B' || phoneNumber.charAt(currentIndex) == 'C' )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == 'D' || phoneNumber.charAt(currentIndex) == 'E' || phoneNumber.charAt(currentIndex) == 'F' )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == 'G' || phoneNumber.charAt(currentIndex) == 'H' || phoneNumber.charAt(currentIndex) == 'I' )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == 'J' || phoneNumber.charAt(currentIndex) == 'K' || phoneNumber.charAt(currentIndex) == 'L' )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == 'M' || phoneNumber.charAt(currentIndex) == 'N' || phoneNumber.charAt(currentIndex) == 'O' )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == 'P' || phoneNumber.charAt(currentIndex) == 'Q' || phoneNumber.charAt(currentIndex) == 'R' || phoneNumber.charAt(currentIndex) == 'S' )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == 'T' || phoneNumber.charAt(currentIndex) == 'U' || phoneNumber.charAt(currentIndex) == 'V' )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == 'W' || phoneNumber.charAt(currentIndex) == 'X' || phoneNumber.charAt(currentIndex) == 'Y' || phoneNumber.charAt(currentIndex) == 'Z' )
chartoNumber = 9;
else
chartoNumber = phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);
}
}
return chartoNumber;
}
public static void main(String[] args) {
boolean validationResult=false;
do {
try{
System.out.println("Enter a phone number to convert:");
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
String phoneNumber = enteredNumber.toUpperCase();
validationResult = isValidPhoneNumber(phoneNumber);
if(validationResult==false){
System.out.println("Entered phone number is incorrect.");
}
System.out.println(validationResult);
input.close();
translatePhoneNumber(phoneNumber, 0, 1);
System.out.print("-");
translatePhoneNumber(phoneNumber, 1, 4);
System.out.print("-");
translatePhoneNumber(phoneNumber, 4, 7);
System.out.print("-");
translatePhoneNumber(phoneNumber, 7, 11);
System.out.println();
}
catch(Exception e){
e.printStackTrace();
}
}while(validationResult==false);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论