Results 1 to 10 of 10
Thread: stringbuffer/stringbuilder help
- 03-09-2012, 01:51 AM #1
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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
-
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?
- 03-09-2012, 02:49 AM #3
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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.
-
Re: stringbuffer/stringbuilder help
- 03-09-2012, 03:00 AM #5
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Re: stringbuffer/stringbuilder help
oh like a recursion? i switched to stringbuilder since they're a little more user friendly in this situation
- 03-09-2012, 03:05 AM #6
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Re: stringbuffer/stringbuilder help
I'm getting an error on line eight above. I'll show you the method being called and the error message....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; }
The error messages says: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(); }
"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?
-
Re: stringbuffer/stringbuilder help
- 03-09-2012, 03:07 AM #8
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Re: stringbuffer/stringbuilder help
think i got the loop down, its just that error saying that my string array is not a string array.....
-
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.
- 03-09-2012, 03:35 AM #10
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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
-
String vs. StringBuilder vs. StringBuffer
By stchman in forum New To JavaReplies: 5Last Post: 01-12-2012, 07:52 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 06:03 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum New To JavaReplies: 1Last Post: 03-03-2009, 05:27 AM -
Difference between StringBuilder & StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 5Last Post: 04-16-2008, 12:51 PM -
StringBuilder v/s StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 9Last Post: 04-11-2008, 09:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks