Question

In: Computer Science

In Java with Indentations Please!!!! 4.16 LAB: Parsing dates Complete main() to read dates from input,...

In Java with Indentations Please!!!!

4.16 LAB: Parsing dates

Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. Any date not following that format is incorrect and should be ignored. Use the substring() method to parse the string and extract the date. The input ends with -1 on a line alone. Output each correct date as: 3/1/1990.

Ex: If the input is:

March 1, 1990
April 2 1995
7/15/20
December 13, 2003
-1

then the output is:

3/1/1990
12/13/2003

Solutions

Expert Solution

Below is your code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class ParsingDates {
        public static void main(String[] args) throws ParseException {
                List<String> validDates = new ArrayList<>();
                Scanner sc = new Scanner(System.in);
                String input = "";
                // list to check if month is valid
                List<String> validMonths = Arrays.asList("January", "February",
                                "March", "April", "May", "June", "July", "August", "September",
                                "October", "November", "December");
                // loop till -1 is entered
                while (!input.equals("-1")) {
                        // getting complete date
                        input = sc.nextLine();
                        // check if -1 is entered or not
                        if (!input.equals("-1")) {
                                // check if date contains spaces
                                if (input.indexOf(' ') != -1) {
                                        // getting the month name
                                        String monthName = input.substring(0, input.indexOf(' '));
                                        // validating if month entered is correct and valid
                                        if (validMonths.contains(monthName)) {
                                                // splitting date to day, month and year
                                                String words[] = input.split(" ");
                                                // checking if date contains all three day, month and
                                                // year
                                                if (words.length == 3) {
                                                        // checking if year is valid and is of 4 letters
                                                        if (words[2].length() == 4) {
                                                                // checking if comma is there after day
                                                                if (words[1].indexOf(',') != -1) {
                                                                        String day = words[1].substring(0,
                                                                                        words[1].indexOf(','));
                                                                        // checking if day is valid
                                                                        if (Integer.parseInt(day) <= 31) {
                                                                                // parsing the month number and year
                                                                                String year = words[2];
                                                                                Date date = new SimpleDateFormat("MMMM")
                                                                                                .parse(monthName);
                                                                                Calendar cal = Calendar.getInstance();
                                                                                cal.setTime(date);
                                                                                String monthNum = (cal
                                                                                                .get(Calendar.MONTH) + 1) + "";
                                                                                // adding the valid date in the list
                                                                                validDates.add(monthNum + "/" + day
                                                                                                + "/" + year);
                                                                        }
                                                                }

                                                        }
                                                }
                                        }
                                }
                        }
                }
                sc.close();
                // printing result
                for (String date : validDates) {
                        System.out.println(date);
                }
        }
}

Output

3/1/1990
12/13/2003

Related Solutions

In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring:...
.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
In C code format please! 4.16 LAB: Checker for integer string Forms often allow a user...
In C code format please! 4.16 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT