• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hello! New here, question understanding some code

 
Christopher Schneider
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic