-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMe.java
More file actions
154 lines (152 loc) · 5.31 KB
/
FindMe.java
File metadata and controls
154 lines (152 loc) · 5.31 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
/*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);
}
}
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;
}
System.out.println("Letter(s) that appear in 1 word:");
System.out.println(word);
}
public void outputWords(String w1, String w2) {
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);
counter+=countLetters(w2, (char)j);
if (counter==2 && word.length()==0) {
word+=(char)j; //this is to check if it's the first letter that works
} else if(counter==2) {
word = word +","+(char)j;
}
counter=0;
}
System.out.println("Letter(s) that appear in 2 words:");
System.out.println(word);
}
public void outputWords(String w1, String w2, String w3) {
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);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
if (counter==3 && word.length()==0) {
word+=(char)j; //this is to check if it's the first letter that works
} else if(counter==3) {
word = word +","+(char)j;
}
counter=0;
}
System.out.println("Letter(s) that appear in 3 words:");
System.out.println(word);
}
public void outputWords(String w1, String w2, String w3, String w4) {
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);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
counter+=countLetters(w4, (char)j);
if (counter==4 && word.length()==0) {
word+=(char)j; //this is to check if it's the first letter that works
} else if(counter==4) {
word = word +","+(char)j;
}
counter=0;
}
System.out.println("Letter(s) that appear in 4 words:");
System.out.println(word);
}
public void outputWords(String w1, String w2, String w3, String w4, String w5) {
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);
counter+=countLetters(w2, (char)j);
counter+=countLetters(w3, (char)j);
counter+=countLetters(w4, (char)j);
counter+=countLetters(w5, (char)j);
if (counter==5 && word.length()==0) {
word+=(char)j; //this is to check if it's the first letter that works
} else if(counter==5) {
word = word +","+(char)j;
}
counter=0;
}
System.out.println("Letter(s) that appear in 5 words:");
System.out.println(word);
}
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;
}
}