Results 1 to 5 of 5

Thread: String help!

  1. #1
    clipper is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default String help!

    Alright so I just began taking a class learning about entry level java programming, and so far decently good but I have run into a problem writing a homework program. In my head this program seems to work. However in eclipse it keeps telling that i can't convert this char to a string. I have to follow these instructions:

    copySubsequence(): this method accepts a String s and two ints a and b as parameters. The
    method returns a new String that contains all the characters to the right of position a and left of
    position b in s, inclusively. For example, if s = abcdef, a = 0, b = 3, then the method will return
    abcd.

    Note: that the characters in a String are numbered starting at 0. If a String has n characters, the
    last character is at position n-1. If you try to access a String character at position 􀀀5, say, your
    program will crash. Similarly, trying to access a String character at position n will also cause the
    program to crash.

    The copySubsequence() method addresses \bad" values of a and b as follows:
    { If a (or b) is less than 0, its value is changed to 0.
    { If a (or b) is greater than or equal to the length n of s, its value is changed to n - 1.
    { If a is greater than b the method returns an empty String (since there is nothing that is both
    to the right of a and to the left of b).
    { If s is empty, the method returns s.

    Here's my attempt at the program

    public class Mutations {

    public static String copySubsequence(String s, int a, int b) {

    if (a < 0) a = 0;
    if (b < 0) b = 0;
    if (a >= s.length()) a = s.length() - 1;
    if (b >= s.length()) b = s.length() - 1;
    if (a > b) return "";
    if (s == "" ) return s;

    while (a <= b) {
    return s.charAt(a);
    a++;
    }
    }

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're trying to return a single char since charAt returns just that, and your while loop will only loop once, since once a return statement is encountered, the method returns the result and immediately ends. Perhaps you want to build a String using a StringBuilder object or else do simple String concatenation within the while loop and then return the String after the while loop has completed itself.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by clipper View Post
    while (a <= b) {
    return s.charAt(a);
    a++;
    }
    That doesn't make sense; try it out (either in your head or on paper). You immediately return the char at position a when your while loop enters its body. You should have an empty String ready to which you append every character at position a in the body of that loop. After the loop has finished you should return that String. (this description of it takes more characters than the actual code ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    My guess would be your function call of copySubsequence looks something like this:

    Java Code:
    String s1 = "abcdef";
    String s2 = copySubsequence(s1,0,3);

    First of all, as Fubarable said, the return statement will be executed once and break out of the loop / method. You'll have to construct the string within the loop, then return the string.

    The error you are receiving is due to "charAt" returning a char, and you're trying to store it into a String.

  5. #5
    clipper is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks very much for your helpful replies. I know some of these questions may have been stupid, but i'm glad that i got some helpful and constructive responses!

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  4. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  5. Replies: 5
    Last Post: 01-11-2009, 10:12 AM

Posting Permissions

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