Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

    Default why is this a bad string

    public class trend {
    public static void main(String[] args) {
    processWord("banana");
    }
    public static void processWord(String s) {
    char c = getLastLetter(s);
    System.out.println(c);
    }
    public static char getLastLetter(String s) {
    int index = s.length();
    char c = s.charAt(index);
    return c;
    }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    931
    Rep Power
    2

    Default Re: why is this a bad string

    length() returns the length (doh) so for "banana" it would be 6. charAt() works with indexes, and these are zero-based. So the last character for "banana" is 5, not 6. In other words, the last character is length()-1.

  3. #3
    ArnavKumar is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: why is this a bad string

    Re: why is this a bad string
    int index = s.length();
    index will be 6 for "banana". But when you use charAt() method it starts with 0 so last character will be at position 5.
    That is why,
    char c = s.charAt(index);
    will not work here and it should be as
    char c = s.charAt(index-1);

  4. #4
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    931
    Rep Power
    2

    Default Re: why is this a bad string

    Quote Originally Posted by ArnavKumar View Post
    Re: why is this a bad string
    int index = s.length();
    index will be 6 for "banana". But when you use charAt() method it starts with 0 so last character will be at position 5.
    That is why,
    char c = s.charAt(index);
    will not work here and it should be as
    char c = s.charAt(index-1);

    Did you just actually copy/paste my reply and hour after I posted it? Seriously?

    Edit: besides, it's massively confusing if you type:
    Java Code:
     int index = s.length();
    char c = s.charAt(index-1)
    You should replace that with:
    Java Code:
     int index = s.length()-1;
    char c = s.charAt(index)
    Last edited by SurfMan; 10-29-2013 at 11:42 AM.

  5. #5
    ArnavKumar is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: why is this a bad string

    Hi SurfMan,
    Do you really think that there is something to copy paste. If you think then what can one do, but i did not do this. I just changed index to index-1 that is it. And your new Java Code illustrates everything about the error. Nothing else to say now.

Similar Threads

  1. Replies: 4
    Last Post: 09-26-2013, 11:35 AM
  2. Replies: 2
    Last Post: 04-28-2013, 01:50 AM
  3. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  4. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  5. Replies: 1
    Last Post: 11-04-2010, 06:53 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
  •