Results 1 to 4 of 4
  1. #1
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default The String Class #2

    "Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence ."

    They have asked me to write an expression. I have given them:

    e

    sentence.substring(7)

    sentence.substring(-1)

    sentence.substring(9)

    sentence.substring(0)

    sentence.str(0)

    sentence.char(0)

    sentence.charAt(0)

    sentence.charAt(e)

    sentence.charAt()

    sentence.charAt()e

    and several others.

    I went to and looked and looked, but the only thing that I saw remotely related to this was the charAt method. I have no idea what I am doing wrong because earlier, I had come across a very similar problem:

    "Given the String variable name, write an expression that evaluates to the third character of name."

    I had entered "name.charAt(2)" and that was the correct answer.

    But wait! There was another:

    "Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the second character of the value of name . So if the value of name were "Smith" the expression's value would be 'm'."

    I had entered "name.charAt(1)" and that was correct. Could someone please help me in any way?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: The String Class #2

    None of those (for the first one) is correct since none of them take into account the length of string 'variable' refers to.

    The second two questions are not quite the same, so don't expect to be able to use exactly the same code.

    All I can suggest is read the API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: The String Class #2

    Quote Originally Posted by Tolls View Post
    None of those (for the first one) is correct since none of them take into account the length of string 'variable' refers to.

    The second two questions are not quite the same, so don't expect to be able to use exactly the same code.

    All I can suggest is read the API.
    So, I'm at least on the right track.

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: The String Class #2

    There are two methods that, when combined, will yield the last letter in a String object. As Tolls suggested, read the API and you'll see which two I mean.

Similar Threads

  1. The String Class #1
    By Everyman in forum New To Java
    Replies: 3
    Last Post: 02-22-2012, 03:39 PM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. String Class
    By madhav1351 in forum Advanced Java
    Replies: 1
    Last Post: 09-27-2010, 08:15 AM
  4. My own string class
    By viperlasson in forum New To Java
    Replies: 8
    Last Post: 01-25-2010, 05:55 PM
  5. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 09:41 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
  •