Given an input file of dates represented as Strings read the

Given an input file of dates represented as Strings, read the dates from the file and display them in a GUI. The dates will be in the form yyyymmdd (such as 20161001 for October 1, 2016). The GUI should have a GridLayout with one row and two columns. The left column should display the dates in the order read from the file, and the right column should display the dates in sorted order (using Selection Sort).
The Strings representing the dates may be compared using the compareTo method in class String. As you are reading the dates you should check that the value read in is legal (8 digits), and if it is not, print it to the console and do not put it in the array of dates.


The input file
Each line of the input file may contain several dates separated by commas. You will need to use a StringTokenizer to separate out the individual dates. So, an example of the input file would be:


20161001
20080912,20131120,19980927
20020202,hello
20120104

Solution

ANSWER:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class DateFromFile {

   public static String[] getDatesFromFile() throws FileNotFoundException {
       String arr[] = new String[20];

       Scanner scan = new Scanner(new File(\"resource/dates.txt\"));
       int size = 0;
       StringTokenizer st = null;

       while (scan.hasNextLine()) {
           st = new StringTokenizer(scan.nextLine(), \",\");
           while (st.hasMoreElements()) {
               String str = (String) st.nextElement();
               if (str.matches(\"^[0-9]{8}$\")) { // matches method
                   arr[size++] = str;
               }

           }
       }
       String unsorted[] = new String[size];
       System.arraycopy(arr, 0, unsorted, 0, size);
       scan.close();
       return unsorted;

   }

   public static void selectionSort(String sorted[], int size) {
       for (int i = 0; i < size; i++) {
           int index = i;
           for (int j = i + 1; j < size; j++) {
               if (sorted[j].compareTo(sorted[index]) < 0) {
                   index = j;
               }
           }
           String smallerNumber = sorted[index];
           sorted[index] = sorted[i];
           sorted[i] = smallerNumber;
       }

   }

   public static void main(String args[]) throws FileNotFoundException {

       String unsorted[] = getDatesFromFile();
       int size = unsorted.length;
       String sorted[] = new String[unsorted.length];
      
       System.arraycopy(unsorted, 0, sorted, 0, size);

       selectionSort(sorted, size);
       for (int j = 0; j < size; j++) {
           System.out.println(unsorted[j] + \" \" + sorted[j]);
       }

   }

}

Points to be note:

1) Please give correct file path where file contains in your PC, otherwise u will get file not found exception

2) File must be of type .txt only

3) System.arraycopy(unsorted, 0, sorted, 0, size) defines copying one array to other

4) To compare date is must 8 digits, I\'m use matches() method

Given an input file of dates represented as Strings, read the dates from the file and display them in a GUI. The dates will be in the form yyyymmdd (such as 201
Given an input file of dates represented as Strings, read the dates from the file and display them in a GUI. The dates will be in the form yyyymmdd (such as 201

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site