Question: Can anyone help me out with this? In this assignment, you will write a Java program that first asks the user for a list of
Can anyone help me out with this? In this assignment, you will write a Java program that first asks the user for a list of dates, and then prints the earliest date and the latest date. Here is an example run of the program (user input is in red, and your print statements are in black): Enter a list of dates: 3/12/2005, 6/16/2015, 4/9/1999, 8/31/2015, 2/1/1999 Earliest date: 2/1/1999 Latest date: 8/31/2015 Requirements This program should contain a single class (called Proj6) with a main method that compiles and runs in BlueJ. Here are some additional requirements: The dates from the user must be stored in an int[][] array. Each row in the array will represent a different date from the list. The first column will represent the month, the second column will represent the day, and the last column will represent the year. For example, in the list of dates above, index [0][1] in your array should be 12. (The 0 means the first date, 3/12/2005, and the 1 means the month for that date -- 12.) You must use a StringTokenizer to break apart the list of dates and store them in your array. A suggested algorithm is provided below. This project is somewhat similar to the "Max and Min" example in the Arrays chapterPreview the documentView in a new window. Just like this example, you will want to keep a current "smallest" date and a current "largest" date. You will need to do more work to see if one date is "less than" another date, though. When finding the earliest and latest dates, remember that several dates can have the same year and the same month. For example, 8/31/2015 is later than 8/16/2015. This means that if you are comparing a date in your array to your current "smallest" date, if the years of the two dates are the same, you will then need to compare the month values and possibly the day values. Here is a suggested algorithm for breaking apart the list of dates into an array: Get the list of dates as a String from the user Use a StringTokenizer, st, on the list, with ", /" (space, comma, slash) as your delimeters Create an array with st.countTokens() rows and 3 columns For each row in the array (start i at 0, use .length to get the number of rows in your array) Set spot [i][0] in your array to the next token from the StringTokenizer (the month) Set spot [i][1] in your array to the next token from the StringTokenizer (the day) Set spot [i][2] in your array to the next token from the StringTokenizer (the year) import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Proj6 { public static boolean isLessThan(int[][] array, int i, int j){ if((array[i][2] < array[j][2]) || (array[i][2] == array[j][2] && array[i][0] < array[j][0]) || (array[i][0] == array[j][0] && array[i][1] < array[j][1])) return true; else return false; } public static void main(String[] args){ Scanner in = new Scanner(System.in); StringTokenizer tokenizer; System.out.print("Enter number of dates: "); int num = in.nextInt(); int[][] array = new int[num][3]; String date; System.out.println("Enter dates: "); for(int i = 0; i < num; ++i){ date = in.next(); tokenizer = new StringTokenizer(date, "/"); if(tokenizer.countTokens() != 3){ System.out.println("Invalid date: " + date); } else{ array[i][0] = Integer.parseInt(tokenizer.nextToken()); array[i][1] = Integer.parseInt(tokenizer.nextToken()); array[i][2] = Integer.parseInt(tokenizer.nextToken()); } } int minInd = 0, maxInd = 0; for(int i = 0; i < num; ++i){ if(isLessThan(array, i, minInd)){ minInd = i; } if(!isLessThan(array, i, maxInd)){ maxInd = i; } } System.out.println("Minimum date is: " + array[minInd][0] + "/" + array[minInd][1] + "/" + array[minInd][2]); System.out.println("Maximum date is: " + array[maxInd][0] + "/" + array[maxInd][1] + "/" + array[maxInd][2]); } } Why won't this work? error on line 12
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
