0

So I'm trying to find the latest date from a list of dates, but I keep getting a NumberFormatException. Is there any way of resolving this?

import java.util.*;

public class Date
{
    private String date;
    private int day;
    private int month;
    private int year;

    public Date(String date)
    {

        String [] newDate = date.split(" ");

        this.day = Integer.parseInt(newDate[0]);
        this.month = Integer.parseInt(newDate[1]);
        this.year = Integer.parseInt(newDate[2]);
    }

    public boolean isOnOrAfter(Date other)
    {
        if(this.day < other.day)
        {
            return true;
        }
        else if(this.day == other.day && this.month < other.month)
        {
            return true;
        }
        else if(this.day == other.day && this.month == other.month && this.year < other.year)
        {
            return true;
        }
        return false;
    }

    public String toString()
    {
        return day + "/" + month + "/" + year;
    }

    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("How many dates: ");

        int num = in.nextInt();

        System.out.println("Enter " + num + " dates: ");

        String [] dates = new String[num];

        for(int i = 0; i < dates.length; i++)
        {
            dates[i] = in.nextLine();
        }

        Date latest = new Date(dates[0]);

        for(int i = 0; i < dates.length; i++)
        {
            Date newDates = new Date(dates[i]);
            if(latest.isOnOrAfter(newDates))
            {
                latest = newDates;
            }
        }
        System.out.println(latest);
    }
}

I think its just a tiny problem, but I can't seem to find it. Thanks in advance.

I have a method that checking for the latest date, the logic of the code seems fine to me, if you see any problems in the logic, let me know.

The dates will be input one line at a time, for example:

1 1 1890
1 1 2000
2 1 2000
30 12 1999

The output should be 2/1/2000.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
blazing
  • 455
  • 1
  • 4
  • 16
  • 2
    You haven't told what you input to it? – user7 Mar 12 '18 at 17:39
  • Example of what you receive in `String date`? – ernest_k Mar 12 '18 at 17:40
  • sorry guys added it just now – blazing Mar 12 '18 at 17:40
  • NumberFormatException is caused by Integer.parseInt() If you split input by space and there are multiple consecutive spaces in the input string the resulting array will contain empty strings. You are trying to parse one of these empty Strings. – MatheM Mar 12 '18 at 17:44
  • Is there a reason why you are using custom Date object instead of `java.util.Date` or `java.time.LocalDate`? – MatheM Mar 12 '18 at 17:47
  • https://stackoverflow.com/questions/10079415/splitting-a-string-with-multiple-spaces `String [] newDate = date.split(" +");` – clinomaniac Mar 12 '18 at 17:48
  • Thanks! So then what would be the best way when it comes to splitting inputs? @MatheM – blazing Mar 12 '18 at 17:50
  • 3
    The question requires you to create your own date class. Sorry should have mentioned that. – blazing Mar 12 '18 at 17:51
  • As an aside, your `isOnOrAfter` doesn’t work correctly. But I expect you will find out soon enough once you’ve solved the `NumberFormatException` problem. – Ole V.V. Mar 12 '18 at 18:39
  • Yeah, i saw that just now. It outputs 30/12/1999, I don't know why. Currently trying to fix it. – blazing Mar 12 '18 at 18:41
  • 1
    You still have a bug in your core logic. You must compare in this order - year, month and day. But you are doing the reverse. Take these two dates for example - Jan 2nd 1990 and Jan 5th 1880 - Try to debug your code with this – user7 Mar 13 '18 at 06:27
  • Thanks very much @user7 – blazing Mar 15 '18 at 15:39

2 Answers2

1

There are two reasons for the NumberFormatException here:

1.You have multiple spaces in your input between the day, month and the year.
2. Scanner's nextInt does not consume the newline. Hence you need to include a dummy nextLine after it. You can read more about this here.

int num = in.nextInt();
in.nextLine(); //To consume the new line after entering the number of dates
System.out.println("Enter " + num + " dates: ");

String [] dates = new String[num];
 ...
user7
  • 14,505
  • 3
  • 35
  • 62
0

NumberFormatException is caused by Integer.parseInt() trying to parse string that is not an integer.

Your example input contains consecutive spaces and if you split input by space and there are multiple consecutive spaces in the input the resulting array will contain empty strings. Integer.parseInt() is trying to parse one of these empty Strings and that is causing the exception.

instead of

String [] newDate = date.split(" ");

use

String [] newDate = date.split(" +");

This will consider multiple spaces as a split token and return only the non space characters.

MatheM
  • 731
  • 6
  • 16
  • Well explained answer, but the main problem was the newline still available in the input after reading the number of dates into `num`. This caused an empty string to be read, which caused the exception. – Ole V.V. Mar 12 '18 at 18:36