• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Hello! New here, question understanding some code  RSS feed

 
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.
 
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.
 
Bartender
Posts: 5167
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
 
Marshal
Posts: 60845
190
  • 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
Marshal
Posts: 60845
190
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!