-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssessedCW1.java
More file actions
206 lines (178 loc) · 6 KB
/
AssessedCW1.java
File metadata and controls
206 lines (178 loc) · 6 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
public class AssessedCW1 {
public static void main(String args[]) throws IOException
{
System.out.println();
File file = new File("Test.txt");
if(!file.exists()) {
System.out.println("File doesn't even exist, sorry I can't analyse it. Please check the file name.");
System.exit(0);
}
if(file.length() == 0) {
System.out.println("The text file is empty, sorry I can't analyse it. Please add something to it.");
System.exit(0);
}
Word word=new Word();
Word chainNode,newNode;
String str="";
FileReader precheck =new FileReader("Test.txt");
FileReader formalread =new FileReader("Test.txt");
char[] c = new char[1];
boolean exist=false;
while((precheck.read(c))!=-1){
String specialstr = "`~@#$%^&*_=+{}[]|\\:;\"<>";
boolean status = specialstr.contains(String.valueOf(c));
if(status){
System.out.println("This text may contain special characters, are you sure you want to continue? yes/no");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer;
answer = br.readLine();
if(answer.equals("yes")){
System.exit(0);
}
System.out.println();
break;
}
}
precheck.close();
while((formalread.read(c))!=-1)
{
//System.out.print(String.valueOf(c));
if(String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
{
chainNode = word;
while(chainNode != null)
{
if(chainNode.value.equalsIgnoreCase(str))
{
chainNode.times++;
exist=true;
break;
}
else
{
chainNode=chainNode.next;
}
}
if(exist==false)
{
newNode=new Word(str,1);
newNode.next=word.next;
word.next=newNode;
str="";
}
else
{
exist=false;
str="";
}
}
else
{
str+=String.valueOf(c);
}
}
formalread.close();
if (str!=""){
chainNode=word;
while(chainNode!=null)
{
if(chainNode.value.equalsIgnoreCase(str))
{
chainNode.times++;exist=true;break;
}
else
{
chainNode=chainNode.next;
}
}
if(exist==false)
{
newNode=new Word(str,1);
newNode.next=word.next;
word.next=newNode;
str="";
}
else
{
exist=false;
str="";
}
}
//int i = 1;
newNode=new Word("",0);
chainNode=word.next;
while(chainNode!=null)
{
if(chainNode.times>=newNode.times)
{
newNode=chainNode;
}
chainNode=chainNode.next;
}
newNode.value = newNode.value.toLowerCase();
System.out.println("The most frequent word is \""+newNode.value+"\", appeared "+newNode.times+" times.");
System.out.println();
chainNode = word;
Boolean oexist = false;
while(chainNode!=null)
{
if(chainNode.times==1)
{
oexist = true;
}
chainNode=chainNode.next;
}
if(oexist == false){
System.out.println("There is no word appears only once.");
System.exit(0);
}
chainNode=word;
System.out.print("Those words appeared only one time: ");
while(chainNode!=null)
{
if(chainNode.times==1)
{
chainNode.value = chainNode.value.toLowerCase();
System.out.print("\""+ chainNode.value+"\"");
}
chainNode=chainNode.next;
}
System.out.print(".");
System.out.println();
}
}
class Word
{
String value;
int times;
Word next;
public Word(String value,int times)
{
this.value=value;
this.times=times;
next=null;
}
public Word() //polymorphism
{
this.value="";
this.times=0;
next=null;
}
}
abstract class Prompt {
abstract void promp();
}
class Prompt0 extends Prompt {
public void promp() {
System.out.println("There is no word appears only once.");
}
}
class Prompt1 extends Prompt {
public void promp() {
System.out.print("Those words appeared only one time: ");
}
}