Question
Hello,
I'm quite stuck on this program...
Im supposed to do the following:
Have the user input a text, then output how many times the letter appears using Arrays and for loops.
Ex:
If the User inputed "Java"
The output would be:
a: 2
j: 1
v: 1
Ex 2:
If the User inputed "Java is cool"
The Output would be:
a: 2
c: 1
i: 1
j: 1
l: 1
o: 2
v: 1
Thank you!
Message was edited by:
JohnnyQ
[523 byte] By [
JohnnyQa] at [2007-10-3 10:51:13]

Look at this example:
String testString = "CaptainMorgan08 is awesome!";
int numOfAs = 0;
for(int i=0; i<testString.length(); i++)
{
if(testString.toLowerCase().charAt(i) == 'a')
{
numOfAs++;
}
}
System.out.println("The number of As found: "+numOfAs);
>
write down the steps you took to produce thisA: 2J: 1V: 1then convert those steps to java
<Gazes into the future and sees 26 for loops>
> <Gazes into the future and sees 26 for loops><shudders>
Thank you Captain morgan, i had also come up something with that prior to posting, i was just wondering if there was a way to do this program without doing 26 for-loops! :D
So what is your question? Do you want someone to program this for you? Looks like a programming course exercise to me. Where did you get stuck?My advice: formulate an algorithm in simple words, break it in tasks, and then start writing the code for each task using the tools
Hello Stefan,I had basically reached the same area that captain morgan had reached, i was just wondering if there was a way to do this program without using 26 FOR-LOOPS.
Yes, there is. Why not writing down, what you would do manually to achieve your goal? Would you count each letter, note down the count and start with the next letter?
> I had basically reached the same area that captain
> morgan had reached, i was just wondering if there was
> a way to do this program without using 26 FOR-LOOPS.
You might not have to. Create an array of chars like so:
char[] alphabet = {a, b, c, d, e, f, g, h, i...};
int[] numOfLetters[] = new int[26];
You could then have a nested for loop that loops through the alphabet.
> You could then have a nested for loop that loops> through the alphabet.*shudder*
> > You could then have a nested for loop that loops> > through the alphabet.> > *shudder*?! You would rather have 26 for loops?
> > *shudder*> > ?! You would rather have 26 for loops?No, but only one loop, namely on the text.
Use just a single array. Look at the following and think about how it can be modified to your needs.char c = 'a';System.out.println(c - 97);
How will you handle capitals. Does "Baby" have 2 b's or 1 B and 1 b?
I convert it to lower case.
I don't know why I even bother helping you as the last time I did, you didn't even seem to care. I gave you three perfectly good, working examples producing the output that you requested. In fact, that thread still has 10 dukes available. [url http://forum.java.sun.com/thread.jspa?threadID=788415&messageID=4480092#4480092] See this thread if you're curious.[/url]
As for this assignment, here is another example. Use it or don't use it, but at least be courteous enough to not ignore it this time.
public class CharCounter {
public static void main(String[] argv) {
String test = "This is a mother frickin test.";
int[] results = new int[26];
for (int i = 0; i < 26; i++) {
results[i] = 0;
}
char[] data = test.toLowerCase().toCharArray();
for (int j = 0; j < data.length; j++) {
if (data[j] >= 97) {
int t = data[j] - 97;
results[t]++;
}
}
for (int n = 0; n < results.length; n++) {
if (results[n] != 0) {
System.out.println(((char)(n + 97)) + ": " + results[n]);
}
}
}
}
> [url http://forum.java.sun.com/thread.jspa?threadID=788415&messageID=4480092#4480092] See this thread if you're curious.[/url]That reminds me. I fixed Stringify, so its not getting deleted!
> > [url
> http://forum.java.sun.com/thread.jspa?threadID=788415&
> messageID=4480092#4480092] See this thread if you're
> curious.[/url]
>
> That reminds me. I fixed Stringify, so its not
> getting deleted!
I saw that... checked on it a couple of days ago to see what ever happened to it. Happy to see that it's still there. ;-)
To avoid an out of bounds exception might want to modify the code.if (data[j] >= 97 && data[j] <= 122) {
My Apologies Navy_Coder, i do appreciate your help.
> My Apologies Navy_Coder, i do appreciate your help.Don't worry about it. Worse things have happened .... ;-)