-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDates.java
More file actions
130 lines (111 loc) · 4.66 KB
/
FindDates.java
File metadata and controls
130 lines (111 loc) · 4.66 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.LinkedList;
import java.util.List;
public class Main1 {
public static void main(String[]args)
{
// April 1, 1976
// April-1-,-1976
List<String> timeline = new LinkedList<>();
List<String> meanings = new LinkedList<>();
String input = "As of May 2013, Apple maintains 408 retail stores. On September 30, 2013, Apple surpassed Coca-Cola to become the world's most valuable brand in the Omnicom Group's 'Best Global Brands' report.";
input = input.replace(".", " .");
input = input.replace(",", " ,");
System.out.println("Input (After minor format changes): \n" +input);
String[] words = input.split(" ");
String[] months = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
for(int i=1;i<words.length;i++)
{
try
{
int a = 0;
if(isNum(words[i])){a = Integer.parseInt(words[i].replaceAll("[a-zA-Z]",""));}
for(int j = 0; j < months.length; j++){
//System.out.println(words[i-1] + " , " + months[j]);
//Date format #1
if(words[i-1].equals(","))
{
try{
if(words[i-3].equals(months[j]))
{
//System.out.println(words[i-3]);
for(int d=1;d<32;d++)
{
//System.out.println(d+words[i-2]);
if(words[i-2].equals(""+d))
{
//System.out.println("Eureka! "+ d);
String meaningWords="";
timeline.add(months[j]+","+a);
for(int m=i+1;m<words.length;m++)
{
if(words[m].equals("."))
break;
else
meaningWords+=words[m]+" ";
}
//System.out.println(meaningWords);
meanings.add(meaningWords);
break;
}
}
}
}
catch (NullPointerException npe)
{
System.out.println("NULL POINTER");
}
}
//Date format #2
if (words[i-1].equals(months[j])){
//if(words[i])
try{
int b = Integer.parseInt(words[i+2]);
}catch(NumberFormatException nfe)
{
//System.out.println();
String meaningWords="";
timeline.add(months[j]+","+a);
for(int m=i+1;m<words.length;m++)
{
if(words[m].equals("."))
break;
else
meaningWords+=words[m]+" ";
}
//System.out.println(meaningWords);
meanings.add(meaningWords);
}
// System.out.println(months[j]+","+a);
}
}
}catch(NumberFormatException nfe)
{
}
}
//clean meanings
for(int i=0;i<timeline.size();i++){
if(meanings.get(i).charAt(0) == ',' || meanings.get(i).charAt(1) == ' ')
meanings.set(i, meanings.get(i).substring(2));
}
System.out.println("Output:");
System.out.println(timeline.size() + " events happend");
for(int i=0;i<timeline.size();i++)
{
System.out.print(timeline.get(i));
System.out.println(" - "+meanings.get(i));
}
}
public static boolean isNum(String in){
if(in.contains("1"))return true;
if(in.contains("2"))return true;
if(in.contains("3"))return true;
if(in.contains("4"))return true;
if(in.contains("5"))return true;
if(in.contains("6"))return true;
if(in.contains("7"))return true;
if(in.contains("8"))return true;
if(in.contains("9"))return true;
if(in.contains("0"))return true;
return false;
}
}