1

I'm totally new to Java programming and I'm trying to create a Java FX project. I've followed tutorials about the date validation method but it seems to fail.In this certain part I have to make a list with objects inserted by a user in text fields. That includes a date but it needs to be valid.

Below in this piece of code, the object I need to get validated is datep . I've created a method in which if the string is valid, it should set my flag to true and return it. Before the list is created I inserted an if statement to check whether that my flag is set to true which means that the date is verified according to the format.When I run it,it creates the list whatsoever even if the date is invalid.Am I putting the if statement in the wrong part? Cause I think the method is fine.

@Override
public void handle(MouseEvent event) {
    if (event.getSource() == NewrentBtn) {
    
    String vehiclen =OximaTxT.getText();
    String clientn = ClientTxT.getText();
    String store = StoreTxT.getText();
    String storer = StorerTxT.getText();
    String timerp = TimeTxT.getText();
    String timer = TimerTxT.getText();
    String datep = DateTxT.getText(); // <-------------
    String dater = DaterTxT.getText();
    Integer sum = Integer.parseInt(SumTxT.getText());    
    if(flag = true) { // <------------
        createRental(id, vehiclen, store, datep, timerp, clientn, storer, dater, timer, sum);
        clearTextFields();
    }
}

public boolean Checkdate(String datep) { // <-------------
    
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date BOD = null;
    df.setLenient(false);
    
    try
    {
        BOD = df.parse(datep); // <----------------
        flag = true;
    }
    catch(Exception e)
    {
        flag = false;
    }
    return flag;
}

public void createRental(int id,String vehiclen,String store,String datep,String timerp,String clientn,String storer,String dater,String timer,int sum ) {
    Rental m = new Rental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
    RentalList.add(m);
    rentalTableView.getItems().add(m);
            
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Dean
  • 45
  • 1
  • 5
  • Hi @Dean i was wondering where are you making the validation of the dateFormat? or in other words where do you call the `public boolean CheckDate(datep)` method before the ìf`validation in the `public void handle(MouseEvent event)` method. – Nicholas Gooding Rios Aug 21 '20 at 01:39
  • Hey there.I see it's missing out from this piece but now that I updated it, I call the method right before the if statement – Dean Aug 21 '20 at 01:50
  • I posted a full solution, let me know if it works. If you need further help let me know and we find a way to tutor you a bit. – Nicholas Gooding Rios Aug 21 '20 at 02:14
  • Oh damn of course..beside the method calling, I inserted a "=" and not "==" inside the if statement.Thanks a lot for helping me out Nicholas!I appreciate the advice as well, I'll take it upon note to have a cleaner structured code – Dean Aug 21 '20 at 02:30
  • java naming conventions please – kleopatra Aug 21 '20 at 04:19

1 Answers1

1

From the looks of what you are trying to achieve here is my suggestion to modify the code.

First of all let me explain to you two issues i found: the first one is that you are missing the call to the validation method of the Date, that is the call to the CheckDate(datep) when you receive the text input and store the flag variable, or so it seems as we dont have the full code (which is ok ); and second you are missing a =in the if(flag = true), it should be if(flag == true)

So here is the full code:

@Override
public void handle(MouseEvent event) {
    if (event.getSource() == NewrentBtn) {

        String vehiclen =OximaTxT.getText();
        String clientn = ClientTxT.getText();
        String store = StoreTxT.getText();
        String storer = StorerTxT.getText();
        String timerp = TimeTxT.getText();
        String timer = TimerTxT.getText();
        String dater = DaterTxT.getText();
        Integer sum = Integer.parseInt(SumTxT.getText());
        String datep = DateTxT.getText();

        boolean flag = Checkdate(datep);
        if(flag == true) {
            createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
            clearTextFields();
        }
    }
}

This way you are verifying if the date is correctly formatted and continue the process if it is according to your scheme.

Finally i have three recommendations as you are new to java programming:

  1. For all methods the first letter should always be in lowercase like public boolean checkDate() this way you can differentiate a method from a Class, which will always start in Uppercase like public class Product. The only exception for this is the constructor of a class.
  2. You should never mix the graphical interface logic, with the logical processing logic. This is: you should keep the processing part in one package and the graphic component in another and relate both of them by creating an instance of the processing logic in the graphical interface.
  3. The user input validation should be directly made in the handler method with try-catch clauses like the following.

Here:

public void handle(MouseEvent event) {
    if (event.getSource() == NewrentBtn) {

        String vehiclen =OximaTxT.getText();
        String clientn = ClientTxT.getText();
        String store = StoreTxT.getText();
        String storer = StorerTxT.getText();
        String timerp = TimeTxT.getText();
        String timer = TimerTxT.getText();
        String dater = DaterTxT.getText();
        Integer sum = Integer.parseInt(SumTxT.getText());
        try {
            String datep = DateTxT.getText();
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            df.parse(date);
            createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
            clearTextFields();
        } catch (ParseException e) {
            /* Here you handle what happens when if fails, you can create a JDialog to show
            the error or create an alert, whatever you need */
            e.printStackTrace();
        }
    }
}

And voila a cleaner version

  • Oh damn of course..beside the method calling, I inserted a "=" and not "==" inside the if statement.Thanks a lot for helping me out Nicholas!I appreciate the advice as well, I'll take it upon note to have a cleaner structured code – Dean Aug 21 '20 at 02:24
  • 2
    @Dean This is one of the reasons you should not test a boolean variable with a boolean literal. Simply use `if (flag)` instead of `if (flag == true)` and use `if (!flag)` instead of `if (flag == false)`. The versions with `==` are discouraged, at least in the Java community. That said, it's acceptable to compare two boolean _variables_ with `!=` (i.e. `if (flag1 != flag2)`) when appropriate; [that's one way to implement an XOR test](https://stackoverflow.com/questions/726652/creating-a-logical-exclusive-or-operator-in-java). – Slaw Aug 21 '20 at 06:46
  • @Slaw is correct, I also do not use the bolean literals but for the sake of your understanding I posted the closest solution to what you already understand. And thank you Slaw for the advice – Nicholas Gooding Rios Aug 21 '20 at 06:49