Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java输入验证_Java - Fatal编程技术网

Java输入验证

Java输入验证,java,Java,因此,我正在使用一个程序,该程序应该包含try-catch块,用于异常处理。我搞不懂的是如何编写一个简单的if语句,用于通过Scanner检查用户的输入,以确保它是一个双精度字符,而不是一个字母或字符,这样,如果是,程序将捕获它,显示错误消息,并告诉用户重新输入另一个值,直到输入合适的输入。我要寻找的是一个简单的if(_width等于一个字母/字符),然后返回false,同时返回一条错误消息,与我已经存在的if语句一起检查输入是否大于零 我目前的代码如下: public class

因此,我正在使用一个程序,该程序应该包含
try-catch
块,用于异常处理。我搞不懂的是如何编写一个简单的
if语句
,用于通过
Scanner
检查用户的输入,以确保它是一个双精度字符,而不是一个字母或字符,这样,如果是,程序将捕获它,显示错误消息,并告诉用户重新输入另一个值,直到输入合适的输入。我要寻找的是一个简单的if(_width等于一个字母/字符),然后返回false,同时返回一条错误消息,与我已经存在的if语句一起检查输入是否大于零

我目前的代码如下:

      public class Rectangle {

//two double data fields width and height, default values are 1 for both.
private double width = 1;
private double height = 1;
private String errorMessage = "";

//no-arg constructor creates default rectangle
public Rectangle() {    
}

//fpzc, called by another program with a statement like Rectangle rec = new Rectangle(#, #);
public Rectangle (double _width, double _height) throws Exception {
    setWidth(_width);
    setHeight(_height);
}

//get functions
public double getArea(){
    return (width * height);
}

public double getPerimeter() {
    return (2*(width + height));
}

public String getErrorMessage() {
    return errorMessage;
}

//set functions
public void setWidth(double _width) throws Exception {
    if( !isValidWidth(_width)){
        Exception e = new Exception(errorMessage);
        throw e;
        //System.out.println(errorMessage);
        //return false;
    }
    width = _width;
}

public void setHeight(double _height) throws Exception {
    if ( !isValidHeight(_height)){
        Exception e = new Exception(errorMessage);
        throw e;
        //System.out.println(errorMessage);
        //return false;
    }
    height = _height;
}

//isValid methods
public boolean isValidWidth(double _width) {

    if(_width > 0){
        return true;
    }
    else {
        errorMessage = "Invalid value for width, must be greater than zero";
        return false;
    }

    if ()

}

public boolean isValidHeight(double _height) {

    if(_height > 0){
        return true;
    }
    else {
        errorMessage = "Invalid value for height, must be greater than zero";
        return false;
    }


}
 }

我的类被另一个我正确编写的测试程序调用。感谢您的帮助!谢谢。

也许这段代码可以帮助您:

double Input=0;


    while(!(Input > 0)){{

    System.out.println("Enter Valid Number");

    Input = new Scanner(System.in).nextDouble();
}

也许这段代码可以帮助您:

double Input=0;


    while(!(Input > 0)){{

    System.out.println("Enter Valid Number");

    Input = new Scanner(System.in).nextDouble();
}
可能是这样的:

    String errorMessage = "error";
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();

    try {
        Double.parseDouble(str);
    }
    catch( Exception e ){
        System.out.println(errorMessage);
    }
或迭代输入并检查每个字符是否为数字:

    String errorMessage = "error";
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    for(int i=0;i<str.length();i++){
        char token = str.charAt(i);
        if(!Character.isDigit(token) && token!='.' ) {
            System.out.println(token + " doesnt work");
            break;
        }
    }
可能是这样的:

    String errorMessage = "error";
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();

    try {
        Double.parseDouble(str);
    }
    catch( Exception e ){
        System.out.println(errorMessage);
    }
或迭代输入并检查每个字符是否为数字:

    String errorMessage = "error";
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    for(int i=0;i<str.length();i++){
        char token = str.charAt(i);
        if(!Character.isDigit(token) && token!='.' ) {
            System.out.println(token + " doesnt work");
            break;
        }
    }

可能重复的。您希望错误消息显示为弹出窗口还是仅显示在控制台中?仅显示控制台,谢谢可能重复的。您希望错误消息显示为弹出窗口还是仅显示在控制台中?仅显示控制台,谢谢我无法接收字符串,用户输入的输入必须进入我的双变量,因此如果我尝试双变量,我会得到一个错误。parseDouble(double)是任何形式的if语句,我可以这样说,例如:如果(_width==[A-z])要捕获输入中包含的字母我不能接受字符串,用户输入的输入必须进入我的双变量,因此,如果我尝试加倍,我会得到一个错误。parseDouble(Double)是任何形式的if语句,我可以说:if(_width==[A-z])来捕获输入是否包含字母