• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting occurrences on an array with a for loop.  RSS feed

 
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think im going in the right direction but my structure is wrong. I want to print the number a certain letter occurs on the letters array.
My loop is looking through the array and identifying how many 'a's there are, but when it comes to the ouput, i get a sequential result.
I   l i k e   J a v a
a is present 1 times
a is present 2 times

i know the 1 times, 2 times its a result of the loop. as it is counting the number of times a is present. the loop goes trhoug finds an a, outputs the println statement,
the loop goes around again finds another a, outputs the println statement. If i could get the println statement outside of the for loop i would avoid this result, but if i do then the variables inside of the loop block are out of scope and cat call them.
Any suggestions?

----jGRASP: operation complete.


 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove your print statement from inside the loop to outside.


 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I just read you latter part of your message, which implies you've already tried my suggestion.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you do know that by doing that, letters[i] its out of scope, and the compiler cant see it. Also count is set to the default 0. so not only will i have a "cannot find symbol error", count would be 0
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish Shivaraj wrote:Remove your print statement from inside the loop to outside.


This is precisely what the OP said doesn't work.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>  but if i do then the variables inside of the loop block are out of scope and cat call them.
Any suggestions?

That's not entirely true. Your only printing the variable count which is declared outside the loop and should have no scoping issues. If you do; can you show us the  error message you get?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few ways you can solve your problem. One is to find an inelegant workaround, more commonly known as a kludge. The other is to organize your program properly and break it down into smaller steps, each one building on top of the next.

Code that searches for a specific letter the way you have done it is very limited in its use: it only works for that one specific letter, 'a'.  A more useful program would allow you to specify any letter to count. The most logical program, IMO, would be one that counts the occurrences of all letters. This is what's called a (histogram)*see correction below.  This way, you separate the logic of counting occurrences from the logic of displaying how many times a specific letter occurs.

Always try to break the problem down into smaller chunks. That way, the solutions you produce are more organized, logical, and manageable.

Edit: It's called a frequency count. A frequency count can be visually represented by a histogram.
 
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
When run you'll get:

Saw this idea a few weeks ago and it stuck with me. Seems a really clever way to count occurrences of A-Z in a string, with a minimum of fuss.

To explain. We are taking advantage of the fact that A-Z in char is 65-90. Therefore by subtracting 65 from the current char you end up with a number between 0-25. This provides you with an array index to store the count for that particular char. To display you just add 65 to the current array index and display the integer held in that slot.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Saw this idea a few weeks ago and it stuck with me. Seems a really clever way to count occurrences of A-Z in a string, with a minimum of fuss.

To explain. We are taking advantage of the fact that A-Z in char is 65-90. Therefore by subtracting 65 from the current char you end up with a number between 0-25. This provides you with an array index to store the count for that particular char. To display you just add 65 to the current array index and display the integer held in that slot.

It may seem clever but it's a little too clever for its own good. There's quite a bit of unnecessary hand waving in that code.

There's no need to cast a char to an int.  That's an automatic widening conversion. So the cast to (int) on line 15 is unnecessary. In fact, line 15 is unnecessary. You can write line 17 as:

Also, using number literals when you're dealing with chars is just getting your geek on for nothing. The char literal for space is ' ' and the char literal for the letter A is (surprise!), 'A'.  Using those instead of 32 and 65, respectively, would make your program much clearer albeit arguably less geeky. Also, the code on lines 21-24 is pretty opaque, i.e., inscrutable.  The variable y is declared way up on line 11, far from where it's actually used and you have to scan the intervening lines to make sure it wasn't set to some other value.  It could have been written more succinctly and more expressively like this:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to using an int[] for the frequency count is to use a Map<Character, Integer>. A TreeMap is a map implementation that also sorts its keys.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But still, it is a clever idea :-)

Thanks for the syntax tips. Much appreciated.
 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A java 8 way, with streams and Maps is for instance:

or for a TreeMap that Junilu mentions,

Some say that this is easier (?) but for this topic perhaps a bit premature...
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright guys,  while i appreciate all the ideas,  im being thaugh to access arrays with for loops. Thats the reason im trying to do this using for loops. I was thinking i could instead count the instances on thr string that would be easier.
But im requiered to count the instances on the array. I dont have any new code to show because im at work.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry rick, I thought your problem was solved with Pauls code. What problem do you still encounter?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original idea, i had is not working. So istill want to know how to go about accessing the a value abd printing it using a for loop.
The way i figured its not good enough.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well a gamble. What if you only increment the counbter if letter[i] == 'a', but only prints it when we have gone through the whole attay? For instance:

and in a similar fashion, but turning it into a method with the char of interest as parameter:
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your first idea might work for my purpose.
I will try that once i get home.
But why the second if?
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote in your opening post:
(...)
i know the 1 times, 2 times its a result of the loop. as it is counting the number of times a is present. the loop goes trhoug finds an a, outputs the println statement,
the loop goes around again finds another a, outputs the println statement. If i could get the println statement outside of the for loop i would avoid this result, but if i do then the variables inside of the loop block are out of scope and cat call them.
Any suggestions?

So I reckoned that you only wanted the println to be output only once. Therefore the second if: when we are at the end of the array, only then do we make this println.

If that is not the problem, then can you elaborate a little bit?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
placing the output statement outside of the for loop and hard coding the a does the trick, the assignment only requiered me to check for an 'a' value for the phrase i like java and if the string value is changed, but always looking for 'a'. Now im going to take it a step further and find a way to not have to hard code the targeted value.




----jGRASP exec: java FinalPrep
I   l i k e   J a v a   a n d   a   a   a
a is present 6 times
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!