I've made a program to count the number of each letters in a phrase
What I want the resulting text to return is
Enter a phrase
"Cow goes to the market"
There are 1 C('s), 3 O('s), 1 W('s), 1 G('s), 3 E('s), 1 S('s), 3 T('s).. /* you get the gist */ in your phrase.
I want the results to be concatenated into a string instead of just a multi-lined report.
import java.util.*;
public class charCount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static int charCount(String letterN)
{ //variable initializations
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = w = x = y = z = 0;
int total = 0;
total += 1;
//for loop
for(int countN = 0; countN < letterN.length(); countN++) {
switch(letterN.charAt(countN)) {
case 'a':
a++;
break;
case 'b':
b++;
break;
case 'c':
c++;
break;
case 'd':
d++;
break;
case 'e':
e++;
break;
case 'f':
f++;
break;
case 'g':
g++;
break;
case 'h':
h++;
break;
case 'i':
i++;
break;
case 'j':
j++;
break;
case 'k':
k++;
break;
case 'l':
l++;
break;
case 'm':
m++;
break;
case 'n':
n++;
break;
case 'o':
o++;
break;
case 'p':
p++;
break;
case 'q':
q++;
break;
case 'r':
r++;
break;
case 's':
s++;
break;
case 't':
t++;
break;
case 'u':
u++;
break;
case 'v':
v++;
break;
case 'w':
w++;
break;
case 'x':
x++;
break;
case 'y':
y++;
break;
case 'z':
z++;
break;
}
}
//Console logs
System.out.println("Below are the individual character counts for your string");
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("E: " + e);
System.out.println("F: " + f);
System.out.println("G: " + g);
System.out.println("H: " + h);
System.out.println("I: " + i);
System.out.println("J: " + j);
System.out.println("K: " + k);
System.out.println("L: " + l);
System.out.println("M: " + m);
System.out.println("N: " + n);
System.out.println("O: " + o);
System.out.println("P: " + p);
System.out.println("Q: " + q);
System.out.println("R: " + r);
System.out.println("S: " + s);
System.out.println("T: " + t);
System.out.println("U: " + u);
System.out.println("V: " + v);
System.out.println("W: " + w);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Z: " + z);
return total;
}
}
My problem is, I can of course concatenate a System.out.println(); with the int vars but it simply prints the number of letters. I don't know ahead of time what people will type so I need my program to detect if the letter = true and thus returns the character and number inside the report.
I've looked for solutions and cannot find a thing. I can't wrap my mind around this yet and frankly I have no idea how to word this inquiry.
Thank you guys ahead of time
Aucun commentaire:
Enregistrer un commentaire