aspose file tools*
The moose likes Beginning Java and the fly likes Hello!  New here, question understanding some code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hello!  New here, question understanding some code" Watch "Hello!  New here, question understanding some code" New topic
Author

Hello! New here, question understanding some code

Christopher Schneider
Greenhorn

Joined: Aug 10, 2011
Posts: 3
Hi all, I'm trying to teach myself Java via books, and everything's going fairly smoothly. I'm currently reading through Sams Teach Yourself Java in 24 Hours. I've tried three different books thus far, and I'm liking this one the most.

However, of course, I'm not understanding exactly how this bit of code works, so that's why I'm here! The chapter I'm on is storing information in Arrays, and the book has us making a program that counts the number of specific letters in quotes. E.g. There are 10 uses of the letter 'A', 20 of the letter 'B', and so on.

Here's the source:
http://workbench.cadenhead.org/book/java-6-24-hours/source/chapter9/Wheel.java

So I do clearly understand the String Phrase part, but I'm having trouble grasping exactly how it's counting the number of letters. This is the code I'm having trouble with:


So I get parts of the code. It's best that I go through the lines that I do understand:
int[] letterCount = new int[26]; I know this is the number of letters in the alphabet.
for (int count = 0; count < phrase.length; count++) { I know this is making the loop go the total number of arrays in phrase
String current = phrase[count];
char[] letters = current.toCharArray(); I'm just not understanding exactly how the lines from here on out work

I'm thinking I just need someone to plainly spell it out for me. I've tried writing down the steps on paper, but it's not making sense. Any help is greatly appreciated I think the main thing I'm having trouble comprehending is phrase[count] and I thought I understood .toCharArray, but apparently I'm not.
James E Baker
Greenhorn

Joined: Jul 28, 2011
Posts: 23
There are two important things to know.

1. The letters that make up a String are stored within that class as a char array. And, therefore, a String can be convert to a char array.

2. When you have a char such as 'A', it has an integer value. 'A' = 65, 'B' = 66, etc...

It's kind of an unusual situation. Just remember to think of 'A' as 65 thru 'Z' as 90.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

In Java, as in many programming languages, char is a numeric type.

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.1


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
. . . and welcome to the Ranch

I personally would prefer to use a book like Head First Java™. Make sure to use the 2nd edition, and you can get a second-hand copy at a very reasonable price.
Christopher Schneider
Greenhorn

Joined: Aug 10, 2011
Posts: 3
Campbell Ritchie wrote:. . . and welcome to the Ranch

I personally would prefer to use a book like Head First Java™. Make sure to use the 2nd edition, and you can get a second-hand copy at a very reasonable price.


Thanks for the responses. Going to have to look it over some more... Need some caffeine

As for HFJ, I already tried that book, and it wasn't for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
What about Core Java2 by Horstmann and Cornell? You will find a review on our books pages. Have a look at a sample chapter somewhere before you buy it. At this stage, I would suggest you only buy part 1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hello! New here, question understanding some code