• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding the index of a character in an array

 
Maksym Trojanowski
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember the indices do not start with 1 ;)
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: there is a long way and a short way. But the long way is more fun.
 
Maksym Trojanowski
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic