How to check user input error when initializing class in java

How to check input error when initializing class in java? I gave an example

public class Date {
private int d;
private int m;
private int y;    

public Date(int d, int m, int y) {
    if(is valid date){
      this.d = d;
      this.m = m;
      this.y = y; //etc
    } else {
      //  ??????
      // throw an exception?
    }   
  }
}
+4
source share
4 answers

You can go in several ways, I suppose:
- You can just throw an exception - think about whether you want to go with checked or unchecked exceptions

-You could do something like desispir.

public class Date {
    private int d;
    private int m;
    private int y;    

public static Date createNewDate(int d, int m, int y) throws IllegalArgumentException {
     // your checks here

     return new Date(int d, int m, int y);
}

private Date(int d, int m, int y) {
    this.d = d;
    this.m = m;
    this.y = y; //etc
  }
}

You can declare exceptions in your creation method.

Check out these links: - Is it good practice to make constructor an exception? - Can constructors throw exceptions in Java?

, ( , ), , .

+2

- :

import java.io.IOException;
public class Date() {
    private int d, m, y;
    public Date(int d, m, y) throws IOException {
        final int currentYear=2014;
        if (d<0||m<0||y<0||d>31||m>12||y>currentYear) throw new IOException();
        //will exit if there is illegal input
        this.d=d;
        //et cetera
}
}

. , , , , , , , , , , - , , .

+1
static ArrayList<Integer> monthsWithThirtyDays=new ArrayList<Integer>(Arrays.asList(new Integer[]{1,3,5,7,8,10,12}));

private int d, m, y;

public static boolean isValidDate(int d, int m, int y) {
    if(y<=0) // Year can't be below or equal to 0
        return false;

    if(!(m>0 && m<=12)) // Month can't be under 0 or above 12
        return false;

    if(monthsWithThirtyDays.contains(m)) { // If the month has 30 days
        if(!(d>0 && d<=30))// Day can't be below 0 or above 30
            return false;
    } else if(m==2){ // If the month is February
        if(y%4==0) { // If it has 29 days
            if(!(d>0 && d<=29)) //Day can't be below 0 or above 29
                return false;
        } else { // If it has 28 days
            if(!(d>0 && d<=28)) // Day can't be below 0 or above 28
                return false;               
        }
    } else { // If the month has 31 days
        if(!(d>0 && d<=31)) // Day can't be below 0 or above 31
            return false;
    }

    return true;
}

public Date(int d, int m, int y) throws Exception {
if(isValidDate(d,m,y)) {
    this.d=d; this.m=m; this.y=y;
} else {
    throw new Exception();
}

}

29- 4 .

, ( )
:) -Charlie

0

you can use the parse method of the SimpleDateFormat class to check if the date is valid or not. the parse method will throw a ParseException if the date is invalid.

public class Date {
private int d;
private int m;
private int y;    

public Date(int d, int m, int y) throws ParseException {
      String dateToValidate = y+"-"+m+"-"+d;

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      sdf.setLenient(false);
      Date date = sdf.parse(dateToValidate);

      this.d = d;
      this.m = m;
      this.y = y;

  }
}
0
source

All Articles