• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kevan Ryan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kevan Ryan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
 
Kevan Ryan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so now I see. Each letter is already assigned a numerical value?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, every letter has a numeric ascii value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic