-3

When I take the string input using next(), I don't get any exception


String a=sc.next()
SimpleDateFormat b=new SimpleDateFormat("dd/MM/yyyy");
b.parse(a);
System.out.println(b.format(b.parse(a)));

But when I take the input using nextLine() method of Scanner class, I get an exception

String a=sc.nextLine()
SimpleDateFormat b=new SimpleDateFormat("dd/MM/yyyy");
b.parse(a);
System.out.println(b.format(b.parse(a)));

How to fix this, I get the same exception when using sc.nextLine() and date format as "hh:mm a"

EsmaeelE
  • 1,681
  • 5
  • 15
  • 22
  • 1
    What exception are you getting? What does the scanner actually read in? is it user input or are you reading data from a file? – OH GOD SPIDERS Feb 08 '21 at 17:08
  • It is user input...java.text.ParseException: Unparseable date: ""...This is the exception I'm getting.Actually the question says " The first line of each test case contains a single time P " and the time is of the form "hh:mm a" (eg 10:43 AM)...Can't figure out how to take this input. – nandamuri pavan Feb 08 '21 at 17:31
  • 2
    O please, stop using `SimpleDateFormat`. That class, just like `Date` and `Calendar`, is outdated and [troublesome](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api). Use classes from the `java.time` package instead. – MC Emperor Feb 08 '21 at 18:07
  • Welcome to Stack Overflow. For better reception of your next questions you will want to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) before posting. – Ole V.V. Feb 08 '21 at 19:44

2 Answers2

1

This problem maybe related to how the Scanner works.
See documentation of next:

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

and of nextLine:

Advances this scanner past the current line and returns the input that was skipped.

The default delimiter includes the newline character (\n).

I assume the input has a leading newline (empty line) that is being removed by next but not by nextLine.

Test:

  • new Scanner("\ntest\n").next() returns "test"
  • new Scanner("\ntest\n").nextLine() returns "" (empty string)

This will happen, for example, if the previous Scanner input was read with nextInt() (or similar) - this does not consume any newline after the number:

var sc = new Scanner("123\n456\n");
var i = sc.nextInt();  // will be 123
var str = sc.nextLine();  // will be the empty string "" (between 123 and \n)
str = sc.nextLine();  // now it is "test"

String used as input for the Scanner since it is easier to describe, same will happen with user input where a newline will be read if user types the return key (usually to end input)

If this is the case, solution is to call nextLine() after reading a number (or even read the number with nextLine() and then parsing it).

For more details, please check this question: Scanner is skipping nextLine() after using next() or nextFoo()?

this answer is based on the comment that the Exception mentions the empty string "" as unparseable
also consider vsfDawg's answer - next() will stop at whitespace character

0

If your input looks like this:

10/12/2020 11:59:59 am

Then the first invocation of the Scanner next() method would return "10/12/2020" while the first invocation of nextLine() would return "10/12/2020 11:59 am". The former matches your DateFormat while the latter does not. That is may be why next() is behaving but you get an Exception with nextLine().

vsfDawg
  • 460
  • 2
  • 6