Results 1 to 5 of 5
  1. #1
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Help making method that tells what character in an array is at index[x]

    Hey everyone,
    I'm working on an assignment that is pretending there is no String class. I have to create some methods that fulfill some functions of the string class. One of them is finding the character at a given index. I do not really know how to start this. Any tips would be great.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Help making method that tells what character in an array is at index[x]

    Start by declaring a function which will accept some variables (decide what variables-I can identify 3)? Also ask yourself: what does the function need to know and what does it need to return? -> a boolean value (to say, yes I have found it or false for no, or return the character if it is found?)
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help making method that tells what character in an array is at index[x]

    Thanks for the help thus far. I guess I am a little confused on how array values work.

    Java Code:
    public char getCharAt(int index) {
    		for (int i=0; i<words.length; i++) {
    			if (index == words.charValue()){
    				
    			}
    		}
    This is what I have so far. I am trying to create a loop that checks the value of the given character with the given index. The charValue section obviously is not right, but I do not know what method to use to find the value of a character.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help making method that tells what character in an array is at index[x]

    If you want to access an element of array in specified index you can do:

    Java Code:
    char c = words[index];
    Make sure that the index is between 0 and array.length - 1.

    Another thing is, in Java we do not have functions. What we have in Java are methods.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Help making method that tells what character in an array is at index[x]

    I agree with wsaryada that to find an element at a particular index of an array, just use the [] operator, that what it's there for.

    Of course you haven't actually said that you are using an array of char as part of your string class. The question would be more meaningful if you made it more precise: ie said what the class looks like so far, and gave an exact description of the behaviour of the method you are trying to write.

Similar Threads

  1. Finding Character at a Certain Index in a String?
    By Yaviel in forum New To Java
    Replies: 3
    Last Post: 02-09-2012, 12:59 AM
  2. Replies: 1
    Last Post: 10-09-2011, 07:57 PM
  3. Array, method to change character places
    By aramiky818 in forum New To Java
    Replies: 6
    Last Post: 09-12-2011, 01:38 AM
  4. Replies: 3
    Last Post: 08-21-2011, 03:25 AM
  5. Replies: 2
    Last Post: 02-24-2009, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •