0

So I am working with a program that is supposed to incorporate try-catch blocks for exception handling. What I can't figure out is how to write a simple if statement for checking input from the user via Scanner to make sure it is a double and not a letter or a character so that if it is the program will catch it, display the error message, and tell the user to re-enter another value until a suitable input is entered. What I am looking for is a simple if(_width equals a letter/character) then return false along with an error message to go along with my already present if statement that checks whether the input is greater than zero.

my current code is below:

      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;
    }


}
 }

My class is being called by another test program that i have written correctly. Any help is appreciated! Thank you.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
S Zapata
  • 43
  • 10

2 Answers2

2

maybe something like:

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

    try {
        Double.parseDouble(str);
    }
    catch( Exception e ){
        System.out.println(errorMessage);
    }

or iterate through the input and check if each character is digit:

    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;
        }
    }

On declaring your scanner you could also:

    double num;
    String errorMessage = "error";
    while(true) {
        Scanner in = new Scanner(System.in);
        if (in.hasNextDouble()) {
            num = in.nextDouble();
            System.out.println(num);
            break;
        }
        else System.out.println(errorMessage);
    } 
Bobas_Pett
  • 591
  • 5
  • 10
1

Maybe this code helps you:

double Input=0;


    while(!(Input > 0)){{

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

    Input = new Scanner(System.in).nextDouble();
}
Usman Nawaz
  • 82
  • 1
  • 4
  • 21