This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Letter counter program. having trouble understanding it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Letter counter program. having trouble understanding it." Watch "Letter counter program. having trouble understanding it." New topic
Author

Letter counter program. having trouble understanding it.

derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Hi, I am having trouble understanding part of this code below. I explain my problem below the code. Any help greatly appreciated. I have previously posted this post here. other post But still have questions. The problem code snippet is in bold.




I don't understand what this line is for



it looks like it points to the lets say E-A position which would be 4, then ++ that which is 5, but I don't know what it is for? please.
I guess bold doesn't work in code mode.

I also don't understand how this outputs the number of letters for each index

System.out.println (": " + lower[letter]);
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Well, it isn't in bold, because the bold tags don't work inside code tags.

Let's have a look.

Note that upper is an array of ints, declared in line 21. So what is between the square brackets is an array index. current is a char, declared in line 24. In lines 31 - 41, there's a loop that goes over all the characters in line. In line 33, the current character is assigned to current.

The data type char works almost like an integer: it's a number that is the Unicode code for a character. You can do arithmetic with chars, and that's what happens between the square brackets: 'A' is subtracted from current. So, you get an array index where A is 0, B is 1, ... Z is 26.

So, upper[current-'A'] refers to one of the integers in the array. If current is 'A', then it is upper[0], if current is 'B', then it's upper[1] etc.

Now, at the end you have the ++ postfix increment operator. This just adds 1 to whatever the operator is applied to and stores the result. Note that something++ means the same as something = something + 1. So, the line upper[current-'A']++ means upper[current-'A'] = upper[current-'A'] + 1.

So, what it does is count the letters in the sentence. Each time it finds an 'A', upper[0] is incremented, each time it finds a 'B', upper[1] is incremented etc. There's another array for the lower-case letters and non-letters are all counted with a single counter.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Thank you Jasper, I am checking out the code again now.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Ok, other than this 5 page code example previously in my book which I just ignored rather than messing with it, this was the hardest code to understand in my book, and it is page 360 only out of 715 pages. LOL. After stretching my brain , and painfully forging new pathways, I have included my comments to explain the code which "I think" are correct, if anyone was curious. Thank you again!

Here it is!



I hope this is correct, I don't want to confuse anyone. thanks. Derek
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Letter counter program. having trouble understanding it.
 
Similar Threads
Word count problem.
Counting one character at a time from a file
HashMap and Vector
operator cannot be applied to ints.
Hello! New here, question understanding some code