1

I'm trying to take input from user in (dd/mm/yy) format, save it in same format and pass it with getter, setter to another class. I have tried this way:

This is what I have tried:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
    Guest gstObject = new Guest();
    try {
        System.out.println("Enter check-in date (dd/mm/yy):");
        String cindate = input.next();
        Date date1 = myFormat.parse(cindate);
        gstObject.setIndate(date1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

With this code I'm getting an exception when I give input in (27012017 or 27 01 2017):

java.text.ParseException: Unparseable date: "27"
  at java.text.DateFormat.parse(DateFormat.java:366)
  at edu.lfa.GuestArrayList.main(GuestArrayList.java:49)

Please point out what mistake I have made.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Zachary Dale
  • 739
  • 4
  • 11
  • 30
  • 1
    Use `input.nextLine()` instead of `input.next()`. Also, you're prompting the user for a different format than your `SimpleDateFormat` actually accepts, so watch out for that. – Zircon Jan 26 '17 at 19:34
  • 1
    So if you want them to enter `dd/mm/yy`, why did you write `myFormat = new SimpleDateFormat("dd MM yyyy");`? – Dawood ibn Kareem Jan 26 '17 at 19:40
  • try entering "27 01 2017" because you date format is "dd MM yyyy" or change the format to "dd/MM/yyyy". – Keaz Jul 12 '18 at 03:50

2 Answers2

1

Here's javadoc for next(), this is what it says:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

Default delimiter is space and hence, it only reads 27. You need to read the whole line by using nextLine(). Following should work:

System.out.println("Enter check-in date (dd/mm/yy):");
String cindate = input.nextLine();
if(null != cindate && cindate.trim().length() > 0){
    Date date1 = myFormat.parse(cindate);
}

Update To check for null or empty string, you need to wrap the parsing code inside if condition.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Metha I changed `String cindate = input.next();` to `String cindate = input.nextLine();` Now i'm getting error without entering date. Error is `java.text.ParseException: Unparseable date: "" at java.text.DateFormat.parse(DateFormat.java:366) at edu.lfa.GuestArrayList.main(GuestArrayList.java:49)` – Zachary Dale Jan 26 '17 at 19:45
  • I have updated my answer to include null and empty string check. Basically, you can skip the parsing if the input is null or just space and show error message. – Darshan Mehta Jan 26 '17 at 19:49
1

Other answers addressed how to use Scanner correctly. I'll address some other issues.

java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes. Avoid the java.util.Date class like the Plague.

Also, you are inappropriately trying to represent a date-only value with a date+time class.

Instead, use java.time.LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

To parse the user's input, specify a formatting pattern to match exactly the expected input from your user.

DateTimeFormatter f = DateTimeFormatter.parse( "dd/mm/uuuu" );

Parse user input.

LocalDate ld = LocalDate.parse( input , f );

On your business class Guest, make the member variable of type LocalDate.

To persist the LocalDate, you can serialize to text in standard ISO 8601 format. Simply call toString.

String output = guest.getLocalDate().toString();

2017-01-23


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154