-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMe-2.java
More file actions
257 lines (254 loc) · 9.4 KB
/
FindMe-2.java
File metadata and controls
257 lines (254 loc) · 9.4 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*Sudipti Dantuluri
* 11.30.2021
* FindMe.java
* The program outputs lower case letters that appear in all the input strings down to one word.*/
import java.util.Scanner;
public class FindMe {
//field variables
Scanner kb;
public FindMe() {
kb = new Scanner(System.in);
}
public static void main (String[]args) {
FindMe fm = new FindMe();
fm.fmMethod();
}
public void fmMethod() {
String w1, w2,w3,w4,w5;
w1=w2=w3=w4=w5="";
int number = 0;
System.out.println("\n\n\n");
System.out.print("Would you like to run Three Strings(1) or Choose your own(2)? ");
int choice = kb.nextInt();
if (choice==1) { //if choice = 1,then just runs with number =3
number = 3;
} else if(choice==2) {
System.out.println("Please enter how many strings you would like to enter? ");
number = kb.nextInt();
} else {
System.out.println("Wrong choice. Choose either 1 or 2");
}
System.out.printf("Please enter %d strings: ", number);
//converts entire word to lowercase
if(number>=1) {w1=kb.next(); w1=lower(w1);}
if(number>=2) {w2=kb.next(); w2=lower(w2);}
if(number>=3) {w3=kb.next(); w3=lower(w3);}
if(number>=4) {w4=kb.next(); w4=lower(w4);}
if(number==5) {w5=kb.next(); w5=lower(w5);}
/* for (int i=number; i >=1;i--) {
if(i==5) outputWords(w1,w2,w3,w4,w5);
if(i==4) outputWords(w1,w2,w3,w4);
if(i==3) outputWords(w1,w2,w3);
if(i==2) outputWords(w1,w2);
if(i==1) outputWords(w1);
} */
if (number==5) outputWords(w1,w2,w3,w4,w5);
if (number==4) outputWords(w1,w2,w3,w4);
if (number==3) outputWords(w1,w2,w3);
if (number==2) outputWords(w1,w2);
if (number==1) outputWords(w1);
}
public String lower(String str) { //checks each letter and converts to lower case
String word="";
for(int i=0;i<str.length();i++) {
if( (int)str.charAt(i)>=65 && (int)str.charAt(i)<=90) word+=(char)((int)str.charAt(i)+32);
else word+=str.charAt(i);
}
return word;
}
public void outputWords(String w1) {
int counter=0;
String word = "";
for(int j = 97;j<=122;j++) {
//checks letter is contained in word and adds 1 if true
counter+=countLetters(w1, (char)j);
if (counter==1 && word.length()==0) {
word+=(char)j; //this is to check if it's the first letter that works
} else if(counter==1) {
word = word +","+(char)j;
}
counter=0;
}
if (word == "") word = "none";
System.out.println("\nLetter(s) that appear in one word:");
System.out.println(word);
}
public void outputWords(String w1, String w2) {
int counter=0;
String word1 = "";
String word2 = "";
for(int j = 97;j<=122;j++) {
//checks letter is contained in word and adds 1 if true
counter+=countLetters(w1, (char)j);
counter+=countLetters(w2, (char)j);
if (counter==1 && word1.length()==0) {
word1+=(char)j; //this is to check if it's the first letter that works
} else if(counter==1) {
word1 = word1 +","+(char)j;
}
if (counter==2 && word2.length()==0) {
word2+=(char)j; //this is to check if it's the first letter that works
} else if(counter==2) {
word2 = word2 +","+(char)j;
}
counter=0;
}
if (word2 == "") word2 = "none";
if (word1 == "") word1 = "none";
System.out.println("\nLetter(s) that appear in 2 words:");
System.out.println(word2);
System.out.println("\nLetter(s) that appear in one word:");
System.out.println(word1);
}
public void outputWords(String w1, String w2, String w3) {
int counter=0;
String word1 = "";
String word2 = "";
String word3 = "";
for(int j = 97;j<=122;j++) {
//checks letter is contained in word and adds 1 if true
counter+=countLetters(w1, (char)j);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
//System.out.printf("c:%d ", counter);
if (counter==1 && word1.length()==0) {
word1+=(char)j; //this is to check if it's the first letter that works
} else if(counter==1) {
word1 = word1 +","+(char)j;
}
if (counter==2 && word2.length()==0) {
word2+=(char)j; //this is to check if it's the first letter that works
} else if(counter==2) {
word2 = word2 +","+(char)j;
}
if (counter==3 && word3.length()==0) {
word3+=(char)j; //this is to check if it's the first letter that works
} else if(counter==3) {
word3 = word3 +","+(char)j;
}
counter=0;
}
if (word3 == "") word3 = "none";
if (word2 == "") word2 = "none";
if (word1 == "") word1 = "none";
System.out.println("\nLetter(s) that appear in all 3 words:");
System.out.println(word3);
System.out.println("\nLetter(s) that appear in 2 words:");
System.out.println(word2);
System.out.println("\nLetter(s) that appear in one word:");
System.out.println(word1);
}
public void outputWords(String w1, String w2, String w3, String w4) {
int counter=0;
String word1 = "";
String word2 = "";
String word3 = "";
String word4 = "";
for(int j = 97;j<=122;j++) {
//checks letter is contained in word and adds 1 if true
counter+=countLetters(w1, (char)j);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
counter+=countLetters(w4, (char)j);
if (counter==1 && word1.length()==0) {
word1+=(char)j; //this is to check if it's the first letter that works
} else if(counter==1) {
word1 = word1 +","+(char)j;
}
if (counter==2 && word2.length()==0) {
word2+=(char)j; //this is to check if it's the first letter that works
} else if(counter==2) {
word2 = word2 +","+(char)j;
}
if (counter==3 && word3.length()==0) {
word3+=(char)j; //this is to check if it's the first letter that works
} else if(counter==3) {
word3 = word3 +","+(char)j;
}
if (counter==4 && word4.length()==0) {
word4+=(char)j; //this is to check if it's the first letter that works
} else if(counter==4) {
word4 = word4 +","+(char)j;
}
counter=0;
}
if (word4 == "") word4 = "none";
if (word3 == "") word3 = "none";
if (word2 == "") word2 = "none";
if (word1 == "") word1 = "none";
System.out.println("\nLetter(s) that appear in all 4 words:");
System.out.println(word4);
System.out.println("\nLetter(s) that appear in all 3 words:");
System.out.println(word3);
System.out.println("\nLetter(s) that appear in 2 words:");
System.out.println(word2);
System.out.println("\nLetter(s) that appear in one word:");
System.out.println(word1);
}
public void outputWords(String w1, String w2, String w3, String w4, String w5) {
int counter=0;
String word1 = "";
String word2 = "";
String word3 = "";
String word4 = "";
String word5 = "";
for(int j = 97;j<=122;j++) {
//checks letter is contained in word and adds 1 if true
counter+=countLetters(w1, (char)j);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
counter+=countLetters(w4, (char)j);
counter+=countLetters(w5, (char)j);
if (counter==1 && word1.length()==0) {
word1+=(char)j; //this is to check if it's the first letter that works
} else if(counter==1) {
word1 = word1 +","+(char)j;
}
if (counter==2 && word2.length()==0) {
word2+=(char)j; //this is to check if it's the first letter that works
} else if(counter==2) {
word2 = word2 +","+(char)j;
}
if (counter==3 && word3.length()==0) {
word3+=(char)j; //this is to check if it's the first letter that works
} else if(counter==3) {
word3 = word3 +","+(char)j;
}
if (counter==4 && word4.length()==0) {
word4+=(char)j; //this is to check if it's the first letter that works
} else if(counter==4) {
word4 = word4 +","+(char)j;
}
if (counter==5 && word5.length()==0) {
word5+=(char)j; //this is to check if it's the first letter that works
} else if(counter==5) {
word5 = word5 +","+(char)j;
}
counter=0;
}
if (word5 == "") word5 = "none";
if (word4 == "") word4 = "none";
if (word3 == "") word3 = "none";
if (word2 == "") word2 = "none";
if (word1 == "") word1 = "none";
System.out.println("\nLetter(s) that appear in all 5 words:");
System.out.println(word5);
System.out.println("\nLetter(s) that appear in all 4 words:");
System.out.println(word4);
System.out.println("\nLetter(s) that appear in all 3 words:");
System.out.println(word3);
System.out.println("\nLetter(s) that appear in 2 words:");
System.out.println(word2);
System.out.println("\nLetter(s) that appear in one word:");
System.out.println(word1);
}
public int countLetters(String str, char ch) {
//this is the method that actually checks if letter is in the word
for(int i=0; i<str.length();i++) {
if(str.charAt(i)==ch) {
return 1;
}
}
return 0;
}
}