-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject1.java
96 lines (94 loc) · 3.56 KB
/
Project1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Much of this class is copied and adjusted from previous PowerPoints
// This imports the StringTokenizer class for use in this class
import java.util.StringTokenizer;
// This creates a new public class called Project1
public class Project1
{
// This creates a new main method
public static void main(String[] args)
{
// This creates a new TextFileInput using the first command-line argument as the file name
TextFileInput tfi = new TextFileInput(args[0]);
// This initializes an arbitrarily large String array to store the dates
String[] dates = new String[1000];
// This uses the readLine method from TextFileInput to store the first String in the file, which is null if the file is empty
String line = tfi.readLine();
// This creates a new unitialized StringTokenizer
StringTokenizer st;
// This sets the initial value for the index of the array to 0
int index = 0;
// This creates an uninitialized String to store each day temporarily before testing and either accepting and storing or rejecting and printing them
String day;
// This performs the actions within as long as the line does not equal null (file has more lines) and index of the array is less than the length of the arbitraily long String array
while(line != null && index < dates.length)
{
// Each time the loop is performed, this creates a initializes the StringTokenizer with the line from the File and "," as parameters
st = new StringTokenizer(line, ",");
// This performs the actions within as long as the StringTokenizer hasMoreTokens in it
while(st.hasMoreTokens())
{
// This temporarily stores the next Token in the StringTokenizer
day = st.nextToken();
// This uses the isValidDate method to make sure the Token has 8 digits
if(isValidDate(day))
{
// If isValidDate is true, the day is stored at the index of the array, and the index increases by 1;
dates[index] = day;
index++;
}
else
{
// If isValidDate is false, the day is printed to the console and not stored in the array
System.out.println(day + " is not a valid date, and was not put in the array.");
}
}
// This gets the next line of the file and stores it
line = tfi.readLine();
}
// If this test is true, it means the exit condition for the while loop was index was not less than the length of the array, which means not all dates were stored in the array
if(line != null)
{
System.out.println("There were more dates than space in the array.");
System.out.println("The array can only store " + dates.length + " dates.");
System.exit(1);
}
String[] sortedDates = new String[index];
for(int i = 0; i < index; i++)
{
sortedDates[i] = dates[i];
}
selectionSort(sortedDates);
createAndDisplayGUI(dates, sortedDates);
}
public static boolean isValidDate(String date)
{
if(date.length() != 8) return false;
for(int i = 0; i < 8; i++)
{
if(!Character.isDigit(date.charAt(i))) return false;
}
return true;
}
private static void selectionSort(String[] array)
{
for(int i = 0; i < array.length - 1; i++)
{
int indexLowest = i;
for(int j = i + 1; j < array.length; j++)
{
if(array[j].compareTo(array[indexLowest]) < 0)
indexLowest = j;
}
if(!array[indexLowest].equals(array[i]))
{
String temp = array[indexLowest];
array[indexLowest] = array[i];
array[i] = temp;
}
}
}
private static void createAndDisplayGUI(String[] array1, String[] array2)
{
DateGUI mainGUI = new DateGUI(array1, array2);
}
}