aspose file tools*
The moose likes Beginning Java and the fly likes Enumerating Letters (a=1,b=2..) 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 "Enumerating Letters (a=1,b=2..)" Watch "Enumerating Letters (a=1,b=2..)" New topic
Author

Enumerating Letters (a=1,b=2..)

Kevan Ryan
Greenhorn

Joined: Feb 08, 2010
Posts: 9
Hey all,

Right now, I'm having issues making a concise way to enumerate individual letters. Currently, I have a series of while loops that compare my specified string with the string 'abcdefghijklmnopqrstuvwxyz" and then using a counter to determine the position the char falls on the alphabet string.

I realize that Javaranchers aren't allowed to provide free code answers, and frankly, that's not what I'm looking for. I was wondering if you guys had any suggestions in order to make a clearer, more concise way to do this.

Thanks!

-Kevan


||
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Kevan, welcome to JavaRanch!

Why don't you post what you have now, and we can help you fix it up?

Be sure to use code tags in your post


When you do things right, people won't be sure you've done anything at all.
Kevan Ryan
Greenhorn

Joined: Feb 08, 2010
Posts: 9
Here's what I have so far.

The program is made to determine whether or not a word is abecedarian, and works in that sense. However, I am planning on reusing a partial of the code for enumerating the letters, which I can't do right now because that code is a jumbled mess. Basically, I need to parse 'abecedarian' finder code from the letter enumeration code.

Thanks for the quick reply
-Kevan



[ EJFH: Break up long comments ]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I'm not really clear.... you're checking to see if the character is in the alphabet and assigning a number to it?

Do you think it might be better to use a HashMap that has letters as a key and numbers as a value? You'll be able to easily check to see if a character is a key in the map, too.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, here's the thing: "char" is an integral type, and you can compare characters directly as if they were integers. If you assume (or check to make sure) that every character in the word is a lowercase ASCII letter, then you could simply loop over the string and check that each letter is greater than or equal to the previous one (assuming that "abecedarian" means that the letters in the word appear in alphabetical order.) Such a program would be a lot simpler and a lot easier to understand.


[Jess in Action][AskingGoodQuestions]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I get it now... they don't just have to BE letters, they need to be in alphabetical order.

I think I might create an array and be sure their int value is between 97 and 122 (lower case alphabet), inclusive... then compare index 0 to 1 and so on....

so like so in my pseudocode:


So you only need one loop, but you're comparing both situations at once.

If you need better or more syntax appropriate code let me know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Janeice DelVecchio wrote:

That's what Ernest was trying to say: you can replace 97 and 122 with their char representations (and skip the cast to int):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevan Ryan
Greenhorn

Joined: Feb 08, 2010
Posts: 9
Again, thanks for all the helpful replies

Because I'm new-ish to Java, I have yet the learn the concept of arrays and stuff, so that's basically out of my league. However, when 'char' is casted into an 'int' type, what number would that char be? Or does that automatically form a=1, b=2, etc? Also, once the 'char' has been casted, would I be allowed to manipulate that int, and then reform it back into a string?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

As Rob said, you won't need to cast because char is able to be evaluated with greater than or less than expressions.

If you want to know what int value it will have, take a look at an ASCII table... you can google it and print one out if you'd like
Kevan Ryan
Greenhorn

Joined: Feb 08, 2010
Posts: 9
Okay, so now I see. Each letter is already assigned a numerical value?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

yes, every letter has a numeric ascii value.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Actually it is goes even farther than that. Each char is a number. Each number is assigned a letter value.

Look at this Java™ Language Specification section where it tells you
The integral types are byte, short, int, and long, . . . and char, whose values are 16-bit unsigned integers representing UTF-16 code units . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enumerating Letters (a=1,b=2..)