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]
# 1

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);

>

CaptainMorgan08a at 2007-7-15 6:16:25 > top of Java-index,Java Essentials,Java Programming...
# 2
write down the steps you took to produce thisA: 2J: 1V: 1then convert those steps to java
Michael_Dunna at 2007-7-15 6:16:25 > top of Java-index,Java Essentials,Java Programming...
# 3
<Gazes into the future and sees 26 for loops>
floundera at 2007-7-15 6:16:25 > top of Java-index,Java Essentials,Java Programming...
# 4
> <Gazes into the future and sees 26 for loops><shudders>
CaptainMorgan08a at 2007-7-15 6:16:25 > top of Java-index,Java Essentials,Java Programming...
# 5
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
JohnnyQa at 2007-7-15 6:16:25 > top of Java-index,Java Essentials,Java Programming...
# 6
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
stefan.schulza at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 7
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.
JohnnyQa at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 8
Use an array or a Map.
floundera at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 9
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?
stefan.schulza at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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.

CaptainMorgan08a at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 11
> You could then have a nested for loop that loops> through the alphabet.*shudder*
stefan.schulza at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 12
> > You could then have a nested for loop that loops> > through the alphabet.> > *shudder*?! You would rather have 26 for loops?
CaptainMorgan08a at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 13
> > *shudder*> > ?! You would rather have 26 for loops?No, but only one loop, namely on the text.
stefan.schulza at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 14
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);
floundera at 2007-7-15 6:16:26 > top of Java-index,Java Essentials,Java Programming...
# 15
How will you handle capitals. Does "Baby" have 2 b's or 1 B and 1 b?
floundera at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 16
I convert it to lower case.
JohnnyQa at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 17

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]);

}

}

}

}

Navy_Codera at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 18
> [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!
CaptainMorgan08a at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 19

> > [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. ;-)

Navy_Codera at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 20
To avoid an out of bounds exception might want to modify the code.if (data[j] >= 97 && data[j] <= 122) {
floundera at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 21
My Apologies Navy_Coder, i do appreciate your help.
JohnnyQa at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...
# 22
> My Apologies Navy_Coder, i do appreciate your help.Don't worry about it. Worse things have happened .... ;-)
Navy_Codera at 2007-7-21 13:30:47 > top of Java-index,Java Essentials,Java Programming...