简体   繁体   English

如何验证用户在 java 中输入的数据

[英]how do I validate a data that user enters in java

I was writing a program in which user is supposed to enter double value but I want to prompt the user again if he/she enters a string value.我正在编写一个程序,其中用户应该输入双精度值,但如果他/她输入字符串值,我想再次提示用户。

This is my code so far:到目前为止,这是我的代码:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    double purchaseAmount = 0; 
    if (input.hasNext("mp")) { 
        do { 
            System.out.println("Enter your loan amount: ");
            purchaseAmount = input.next(); 
        } while(!input.hasNextDouble()); 
    }
}
    

When you prompt a user in Java (assuming you're using System.in or a JTextField) what you're reading from user input is text.当您在 Java(假设您使用 System.in 或 JTextField)中提示用户时,您从用户输入中读取的是文本。

If you want to convert the text to a double there are several approaches, but you could use:如果要将文本转换为双精度,有几种方法,但您可以使用:

do {
   String promptValue = prompt();
   NumberFormat nf = NumberFormat.getInstance();
   nf.setMaximumFractionDigits(16); // maximum for double
   Number num = nf.parse(promptValue):
} while (null == num);

Notice also that you could ask character by character and parse progressively so you don't need to prompt again but rather discard anything that doesn't match.另请注意,您可以逐个字符询问并逐步解析,因此您无需再次提示,而是丢弃任何不匹配的内容。 This would work well in Swing or you could use jline3 if you really want to get sophisticated.这在 Swing 中可以很好地工作,或者如果您真的想要变得复杂,您可以使用jline3

To learn how to parse progressively look at: https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html要了解如何逐步解析,请查看: https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.ZFC35FDC70D5FC69D269883A822C7A53E

If the input given by the user doesn't match the expected format then an exception will be thrown, you can catch this exception to again request for the long value.如果用户给出的输入与预期的格式不匹配,则会抛出异常,您可以捕获此异常以再次请求 long 值。

    public static Long getLongInputFromUser(){
        Scanner sc= new Scanner(System.in);
        try {
            System.out.println("Print the long value: ");
            Long inputInteger = sc.nextLong();
            return inputInteger;
        }catch(InputMismatchException inputMismatchException){
            System.out.println("The input is not a valid long, please enter again");
            return getLongInputFromUser();
        }
    }

    public static void main(String[] args) {
        Long input = getLongInputFromUser();
        System.out.println("The final value is " + input);
    }

The output of this program is:该程序的output为:

Print the long value: 
abc 
The input is not a valid long, please enter again 
Print the long value: 
pqr 
The input is not a valid long, please enter again
Print the long value: 
6 
The final value is 6 

You can use Java exception mechanism可以使用 Java 异常机制

Scanner input=new Scanner(System.in);
double purchaseAmount;
while(true){
    try{
        System.out.println("please input a double value");
        purchaseAmount=input.nextDoble();
    }catch(Exception e){
        System.out.println("You input a string,not a double value");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM