File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Finding the index of a character in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding the index of a character in an array" Watch "Finding the index of a character in an array" New topic
Author

Finding the index of a character in an array

Maksym Trojanowski
Greenhorn

Joined: Jan 29, 2012
Posts: 12
Yes, like the subject says: How do i get the index of a character in an array? Help would be appreciated XD
given that this is the array. How would i find the index of 'm' for example?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
Do you have any guesses? How do you do it in your head or with a piece of paper and a pencil? Can you describe that process and then program it?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10354
    
    8

And remember the indices do not start with 1 ;)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Hint: there is a long way and a short way. But the long way is more fun.
Maksym Trojanowski
Greenhorn

Joined: Jan 29, 2012
Posts: 12
Greg Brannon wrote:Do you have any guesses? How do you do it in your head or with a piece of paper and a pencil? Can you describe that process and then program it?
well this is my program so far The problem is at line 21, i just don't know the thingie to call the character in the array. This is basically a program that adds up the letter scores of a SCRABBLE word and don't worry about the EasyReader thing
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
Context is always helpful. There are many ways (I'm not going to count them) to do this problem, but starting with a char[] array may not be at the top of my list. Do you have to use a char[] array? Did you consider just using a String = "abcdefg......z" with its available methods? Or, even better (some would say), a HashMap or other collection that returns a value for an input?
Maksym Trojanowski
Greenhorn

Joined: Jan 29, 2012
Posts: 12
Greg Brannon wrote:Context is always helpful. There are many ways (I'm not going to count them) to do this problem, but starting with a char[] array may not be at the top of my list. Do you have to use a char[] array? Did you consider just using a String = "abcdefg......z" with its available methods? Or, even better (some would say), a HashMap or other collection that returns a value for an input?
Never mid you are a genius. I don't know why i didn't think of it first
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
Okay, easy enough.

So you'll have to write your own public static int indexOf( char y ) method. How that goes, roughly, is you compare each element of the char[] array at a time to the desired character, y, in a loop that iterates 0 - the length of the array, and when you find a match, you return the value of the loop's index variable.

(That was the kind of answer I was hoping to get to my first question.)

Can you code that?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
Maksym Trojanowski wrote:
Greg Brannon wrote:Context is always helpful. There are many ways (I'm not going to count them) to do this problem, but starting with a char[] array may not be at the top of my list. Do you have to use a char[] array? Did you consider just using a String = "abcdefg......z" with its available methods? Or, even better (some would say), a HashMap or other collection that returns a value for an input?
Never mid you are a genius. I don't know why i didn't think of it first

Not sure what I did to deserve that, but let's part to meet another time as friends. Good luck.
Maksym Trojanowski
Greenhorn

Joined: Jan 29, 2012
Posts: 12
Greg Brannon wrote:Okay, easy enough.

So you'll have to write your own public static int indexOf( char y ) method. How that goes, roughly, is you compare each element of the char[] array at a time to the desired character, y, in a loop that iterates 0 - the length of the array, and when you find a match, you return the value of the loop's index variable.

(That was the kind of answer I was hoping to get to my first question.)

Can you code that?
I will know that for next time, and yes i can XD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
I would disagree. There is no need to seek the index of 'q' in that array. Remember that a char is not a letter, but a number, so you can find the index easily by doing arithmetic on the chars.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding the index of a character in an array