I need to ask the user for 3 dates (mm/dd/yyyy) and then it needs to print the earliest, latest and the average year. I am TERRIBLE at this & am taking the class because I have to. I don't know how to compare dates any other way than how you would compare numbers (<,>,==). Please help me fix this.

import java.util.*;

public class proj6 {
   public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter 3 dates: ");
        String list = s.nextLine();

        StringTokenizer st = new StringTokenizer(list, ", ");
        String[] dates = new String[st.countTokens()];

        for (int i = 0; i < dates.length; i++) {
            dates[i] = st.nextToken();
        }
        
        proj6.printEarliest(dates);
        int[] arr= new int[0];
        proj6.printLatest(dates);
        int[] arr2= new int[0];
        proj6.printAvgYear(dates);
        int[] arr3= new int[0];
    }

    public static int getMonth(String date) {
        StringTokenizer st = new StringTokenizer(date, "/");
        return Integer.parseInt(st.nextToken());
    }

	
    public static int getDay(String date) {
        StringTokenizer st = new StringTokenizer(date, "/");
        st.nextToken();
        return Integer.parseInt(st.nextToken());
    }

    public static int getYear(String date) {
        StringTokenizer st = new StringTokenizer(date, "/");
        st.nextToken();
        st.nextToken();
        return Integer.parseInt(st.nextToken());
    }

    public static void printEarliest(String[] dates) {
        Scanner s= new Scanner(System.in);
        int getMonth;
        int getDay;
        int getYear;
        int currentDay=getDay(dates[0]);
        int currentMonth=getMonth(dates[0]);
        int currentYear=getYear(dates[0]);
       System.out.print(getMonth(dates[0])+"/"+getDay(dates[0])+"/"+getYear(dates[0]));
        int indexearliest = 0;
      for(int i = 0; i < dates.length; i++)
       {
        
      if (dates[indexearliest] <= (dates[i]));
      {
        indexearliest = i;
      }  
    } 
    
        System.out.println("Earliest date: "+i);
      
    }

    public static void printLatest(String[] dates) {
        Scanner s= new Scanner(System.in);
        int getMonth;
        int getDay;
        int getYear;
        int currentDay=getDay(dates[0]);
        int currentMonth=getMonth(dates[0]);
        int currentYear=getYear(dates[0]);
        System.out.print(getMonth(dates[0]) + "/"+ getDay(dates[0]) + "/"+ getYear(dates[0]));
       
        int indexlatest = 0;
      for(int i = 0; i < dates.length; i++)
       {
      if (dates[indexlatest]<= compareTo(dates[i]));
      {
        indexlatest = i;
       }
     }

        System.out.println("Latest date: "+ i);
    
    }

    public static void printAvgYear(String[] dates) {
        Scanner s= new Scanner(System.in);
        int getYear;
        int currentYear=getYear(dates[0]);
        System.out.print(getYear(dates[0]));
        int indexyear = 0;
      for(int i = 0; i < dates.length; i++)
       {
      if(dates[indexyear]=dates[i])
      {
        indexyear=indexyear/3;
        indexyear = i;
       }
     }
        System.out.println("Average year: "+i);
        
    }
}

First of all, a StrinTokenizer is depricated. You should look into String.split() (http://www.rgagnon.com/javadetails/java-0438.html), which returns an array of Strings.

System.out.print(getMonth(dates[0])+"/"+getDay(dates[0])+"/"+getYear(dates[0]));

Seems a little strange to me. Why not just:

System.out.print(dates[0]);

Date manipulation in Java is done with the Calendar class. You can just call the after() method to check if a date is after another date.

The new code:

public static int getMonth(String date) {
		return Integer.parseInt (date.split ("/")[0]);
	}
	public static int getDay(String date) {
		return Integer.parseInt (date.split ("/")[1]);
	}
	public static int getYear(String date) {
		return Integer.parseInt (date.split ("/")[2]);
	}

/* --- */
public static void printLatest(String[] dates) {
		Calendar[] calendars = new Calendar[dates.length];
		
		for (int i=0; i<dates.length; i++) {
			calendars[i] = Calendar.getInstance ();
			calendars[i].set (getYear(dates[i]), getMonth(dates[i]), getDay(dates[i]));
		}

		//System.out.print (dates[0]);

		Calendar lastDate = calendars[0];
		for (int i = 0; i < dates.length; i++) {
			if (calendars[i].after (lastDate))
				lastDate = calendars[i];
		}

		System.out.println ("Latest date: " + lastDate.get (Calendar.MONTH) + "/" + lastDate.get (Calendar.DATE) + "/" + lastDate.get (Calendar.YEAR));
	}

orrrr ... you could check out the
.equals
.before
.after

methods :)

Thats what I said:

You can just call the after() method to check if a date is after another date.

if (calendars.after (lastDate))

missed that, my bad.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.