This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Finding the index of a character in an array

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And remember the indices do not start with 1 ;)
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79656
381
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Water proof donuts! Eat them while reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic