重新提示,直到正确的数据格式输入。

huangapple go评论79阅读模式
英文:

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(&quot;\\d{4}\\w{7}&quot;))
return true;
else if (phoneNumber.matches(&quot;\\d{1}\\d{3}\\w{7}&quot;))
return true;
else
return false;
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = &#39;A&#39;;
int currentIndex;	
if (firstIndex == 0) {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
chartoNumber =  phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}	
} 
else if (firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
chartoNumber =  phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}	
} 
else {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == &#39;A&#39; || phoneNumber.charAt(currentIndex) == &#39;B&#39; || phoneNumber.charAt(currentIndex) == &#39;C&#39; )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == &#39;D&#39; || phoneNumber.charAt(currentIndex) == &#39;E&#39; || phoneNumber.charAt(currentIndex) == &#39;F&#39; )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == &#39;G&#39; || phoneNumber.charAt(currentIndex) == &#39;H&#39; || phoneNumber.charAt(currentIndex) == &#39;I&#39; )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == &#39;J&#39; || phoneNumber.charAt(currentIndex) == &#39;K&#39; || phoneNumber.charAt(currentIndex) == &#39;L&#39; )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == &#39;M&#39; || phoneNumber.charAt(currentIndex) == &#39;N&#39; || phoneNumber.charAt(currentIndex) == &#39;O&#39; )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == &#39;P&#39; || phoneNumber.charAt(currentIndex) == &#39;Q&#39; || phoneNumber.charAt(currentIndex) == &#39;R&#39; || phoneNumber.charAt(currentIndex) == &#39;S&#39; )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == &#39;T&#39; || phoneNumber.charAt(currentIndex) == &#39;U&#39; || phoneNumber.charAt(currentIndex) == &#39;V&#39; )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == &#39;W&#39; || phoneNumber.charAt(currentIndex) == &#39;X&#39; || phoneNumber.charAt(currentIndex) == &#39;Y&#39; || phoneNumber.charAt(currentIndex) == &#39;Z&#39; )
chartoNumber = 9;
else
chartoNumber =  phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);		
}
}
return chartoNumber;	
}	
public static void main(String[] args) {
System.out.println(&quot;Enter a phone number to convert:&quot;);
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(&quot;-&quot;);
translatePhoneNumber (phoneNumber, 1, 4);
System.out.print(&quot;-&quot;);
translatePhoneNumber (phoneNumber, 4, 7);
System.out.print(&quot;-&quot;);
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(&quot;Wrong Format&quot;);
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(&quot;\\d{4}\\w{7}&quot;))
return true;
else if (phoneNumber.matches(&quot;\\d{1}\\d{3}\\w{7}&quot;))
return true;
else
return false;
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = &#39;A&#39;;
int currentIndex;
if (firstIndex == 0) {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
chartoNumber =  phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else if (firstIndex == 1) {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
chartoNumber =  phoneNumber.charAt(currentIndex);
char number = (char) chartoNumber;
System.out.print(number);
}
}
else {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == &#39;A&#39; || phoneNumber.charAt(currentIndex) == &#39;B&#39; || phoneNumber.charAt(currentIndex) == &#39;C&#39; )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == &#39;D&#39; || phoneNumber.charAt(currentIndex) == &#39;E&#39; || phoneNumber.charAt(currentIndex) == &#39;F&#39; )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == &#39;G&#39; || phoneNumber.charAt(currentIndex) == &#39;H&#39; || phoneNumber.charAt(currentIndex) == &#39;I&#39; )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == &#39;J&#39; || phoneNumber.charAt(currentIndex) == &#39;K&#39; || phoneNumber.charAt(currentIndex) == &#39;L&#39; )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == &#39;M&#39; || phoneNumber.charAt(currentIndex) == &#39;N&#39; || phoneNumber.charAt(currentIndex) == &#39;O&#39; )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == &#39;P&#39; || phoneNumber.charAt(currentIndex) == &#39;Q&#39; || phoneNumber.charAt(currentIndex) == &#39;R&#39; || phoneNumber.charAt(currentIndex) == &#39;S&#39; )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == &#39;T&#39; || phoneNumber.charAt(currentIndex) == &#39;U&#39; || phoneNumber.charAt(currentIndex) == &#39;V&#39; )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == &#39;W&#39; || phoneNumber.charAt(currentIndex) == &#39;X&#39; || phoneNumber.charAt(currentIndex) == &#39;Y&#39; || phoneNumber.charAt(currentIndex) == &#39;Z&#39; )
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(&quot;Enter a phone number to convert:&quot;);
Scanner input = new Scanner(System.in);
String enteredNumber = input.next();
String phoneNumber = enteredNumber.toUpperCase();
validationResult = isValidPhoneNumber(phoneNumber);
if(validationResult==false){
System.out.println(&quot;Entered phone number is incorrect.&quot;);
}
System.out.println(validationResult);
input.close();
translatePhoneNumber(phoneNumber, 0, 1);
System.out.print(&quot;-&quot;);
translatePhoneNumber(phoneNumber, 1, 4);
System.out.print(&quot;-&quot;);
translatePhoneNumber(phoneNumber, 4, 7);
System.out.print(&quot;-&quot;);
translatePhoneNumber(phoneNumber, 7, 11);
System.out.println();
}
catch(Exception e){
e.printStackTrace();
}
}while(validationResult==false);
}
}

huangapple
  • 本文由 发表于 2020年10月27日 12:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64548158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定