Results 1 to 10 of 10
  1. #1
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default stringbuffer/stringbuilder help

    I need to turn an array of strings into a string to be returned. The only problem is that it needs to go like this:

    string[0] + ", " + string[1] +.......... + " and " + string[last]

    the loops I'm writing aren't working correctly and I'm a little rusty with StringBuffer. Could someone help me write a short method to do this?

    Thanks

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

    Default Re: stringbuffer/stringbuilder help

    Sure -- but you first! :)

    Seriously though, don't you think it would be much better for you to show us your attempt so we can see what you might be doing wrong and so you can exercise those "rusty" muscles?

  3. #3
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: stringbuffer/stringbuilder help

    I guess i worded that badly...
    I can get a stringbuffer to build a string out of a string array, but i'm not sure how to change it so that the last connecting part will be " and " instead of ", ". I'll put up what i have so far.

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

    Default Re: stringbuffer/stringbuilder help

    Quote Originally Posted by jcarosella10 View Post
    I guess i worded that badly...
    I can get a stringbuffer to build a string out of a string array, but i'm not sure how to change it so that the last connecting part will be " and " instead of ", ". I'll put up what i have so far.
    Hint: You know the length of the array -- try to use that information to your advantage.

  5. #5
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: stringbuffer/stringbuilder help

    oh like a recursion? i switched to stringbuilder since they're a little more user friendly in this situation

  6. #6
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: stringbuffer/stringbuilder help

    Java Code:
     public Textbook(int pages, String name, int Numberauthors, boolean hard, String pc, String subject)
        {
            super(pages);
            numpages = pages;
            title = name;
            numauthors = Numberauthors;
            String[] authors = new String[numauthors];
            allauthors = makeString(authors[numauthors], numauthors);
            hardCover = hard;
            open = false;
            pubComp = pc;
            field = subject;
        }
    I'm getting an error on line eight above. I'll show you the method being called and the error message....
    Java Code:
    private String makeString(String[] authorlist, int num)
        {
           StringBuilder builder = new StringBuilder(authorlist[0]);
           for(int counter = 0; counter < num; counter++)
           {
               if((counter + 1) != num)
                    builder.append(", " + authorlist[counter]);
               else
                    builder.append(" and " + authorlist[counter]);
           }
           return builder.toString();
        }
    The error messages says:
    "method makeString in class Textbook cannot be applied to given types;
    required: java.lang.String[], int found: java.lang.String, int reason: actual argument java.lang.String cannot be converted to java.lang.String[]"
    I'm getting that error even though authors is an array. Is this error my fault?

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

    Default Re: stringbuffer/stringbuilder help

    Quote Originally Posted by jcarosella10 View Post
    oh like a recursion? i switched to stringbuilder since they're a little more user friendly in this situation
    No, you wouldn't use recursion at all here. You'd use a for loop, looping through the array loop, and the behavior of the code in the loop will be different depending on what the loop index is...

  8. #8
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: stringbuffer/stringbuilder help

    think i got the loop down, its just that error saying that my string array is not a string array.....

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

    Default Re: stringbuffer/stringbuilder help

    makeString accepts a String array and an int. You are passing only a String into it, not an array of String. But a bigger problem I see is that I don't see any authors array being passed in as a parameter to your Textbook class's constructor.

  10. #10
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: stringbuffer/stringbuilder help

    It compiles now, thanks for catching that. I got lost in all the inherited nonsense
    Java Code:
    public class Textbook extends Book
    {
        private int numpages;
        private String title;
        private int numauthors;
        private String allauthors;
        private String[] authors;
        private boolean hardCover;
        private boolean open;
        private String pubComp;
        private String field;
        public Textbook(int pages, String name, String[] xauthors, int Numberauthors, boolean hard, String pc, String subject)
        {
            super(pages);
            numpages = pages;
            title = name;
            numauthors = Numberauthors;
            authors = xauthors;
            allauthors = makeString(authors, numauthors);
            hardCover = hard;
            open = false;
            pubComp = pc;
            field = subject;
        }
        public int numPages()
        {
            return numpages;
        }
        public String bookTitle()
        {
            return title;
        }
        public String authorName()
        {
            return allauthors;
        }
        public boolean isHardCover()
        {
            return hardCover;
        }
        public boolean isOpen()
        {
            return open;
        }
        public void open()
        {
            open = true;
        }
        public void close()
        {
            open = false;
        }
        public String publishingCompany()
        {
            return pubComp;
        }
        private String makeString(String[] authorlist, int num)
        {
           StringBuilder builder = new StringBuilder(authorlist[0]);
           for(int counter = 0; counter < num; counter++)
           {
               if((counter + 1) != num)
                    builder.append(", " + authorlist[counter]);
               else
                    builder.append(" and " + authorlist[counter]);
           }
           return builder.toString();
        }
    }

Similar Threads

  1. String vs. StringBuilder vs. StringBuffer
    By stchman in forum New To Java
    Replies: 5
    Last Post: 01-12-2012, 08:52 AM
  2. Replies: 3
    Last Post: 03-04-2009, 07:03 AM
  3. Replies: 1
    Last Post: 03-03-2009, 06:27 AM
  4. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 01:51 PM
  5. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 10:38 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
  •