-1

I'm trying to convert a String to a Date variable in Java. To attempt to do so I'm trying a parse approach, which needs a try-catch block, but I'm having scope issues. The variable "date" in my Employee constructor isn't receiving the date, due to the date being in the try block. I receive the error: "date cannot be resolved to a variable". -- Please let me know your thoughts, or if I can convert the string to Date in an easier way. Thank you!

/Code Snippet/

           String stringDate = sc.next();
           SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
           
           try {
               Date date = dateFormatter.parse(stringDate);
               
           }catch(ParseException e) {e.printStackTrace();}
         

           e = new Employee(id, login, salary, date, name); 
mak95
  • 21
  • 5
  • What do you want to happen in case the user entered an incorrect date and parsing fails? I suggest that letting the user either try again or cancel the creation of the employee would be a good avenue in most cases. If the `Employee` constructor requires a date and you haven’t been able to obtain a date, then you can’t create the employee, neither should you want to. – Ole V.V. Oct 29 '21 at 03:27
  • I strongly recommend that you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. If that’s a date of birth or a hire date, use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Oct 29 '21 at 03:30
  • A simple solution is to move the creation of the `Employee` object inside the `try` part. Then it will only be created after successful parsing. – Ole V.V. Oct 29 '21 at 03:32
  • Friend Stackers, I think that what would help mak95 the best would be an example of how to take user input and parse into a `LocalDate` in a loop until a valid date is entered. Who can find such an example? It’s gotta be somewhere on Stack Overflow and/or elsewhere. – Ole V.V. Oct 29 '21 at 03:46
  • There’s a similar question here: [do while loop.. to proceed if correct input is given](https://stackoverflow.com/questions/15998135/do-while-loop-to-proceed-if-correct-input-is-given). – Ole V.V. Oct 29 '21 at 06:20
  • @OleV.V. thank you so much for all your feedback. I'm new to java and programming so this is helpful. – mak95 Oct 31 '21 at 01:11

1 Answers1

0

You need to define the variable outside the try scope, then use it from inside. This code will work to your expectation:

String stringDate = sc.next();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;        

try {
    date = dateFormatter.parse(stringDate);
} catch(ParseException e) {
    e.printStackTrace();
}

e = new Employee(id, login, salary, date, name); 

You need to watch out now as while you create the new employee the date might be null. Therefore you might even go one step further and do that only if the date is not null. But in the snippet you have here the variable e is not defined anyway (or just used in the catch block).

String stringDate = sc.next();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

try {
    Date date = dateFormatter.parse(stringDate);
    e = new Employee(id, login, salary, date, name); 
} catch(ParseException e) {
    e.printStackTrace();
}
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Helping to create an employee with a `null` date in case of a parsing error — isn’t that just pushing the problem in front of us? (Also no one in their right mind wants to use `SimpleDateFormat` and `Date`.) – Ole V.V. Oct 29 '21 at 06:22