Results 1 to 6 of 6
Thread: I need some help
- 12-07-2010, 07:35 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
I need some help
Ok. I'm new to this site and I kinda just watch all the post to see if something would help me but its not happening. So I decided to sign up and stop lurking.
So heres my dilemma, my prof. gave us a assignment were we have to work with arrays. A friend of mines was telling me to use a StringBuilder class because it so much easier (which it is) When showing my prof. he told me thats not what he wants because we haven't learned that yet :mad:
**So my question is, how do I turn "" System.out.println((new StringBuilder()) .append("The list capacity is ").append(cap).toString()); ""
**back into System.out.println("the list capacity is " + cap; with it making sense :confused:
I did my whole program using a StringBuilder just for it to be wrong. I'm not trying to ask my friend because I feel bad :(
My code:
import java.io.*;
import java.util.Scanner;
public class JambaList {
private Jamba list[];
private int cap;
private int size;
private static Scanner console;
static {
console = new Scanner(System.in);
}
public JambaList() {
cap = 10;
size = 0;
list = new Jamba[cap];
}
public void printCapcity() {
System.out.println((new StringBuilder())
.append("The list capacity is ").append(cap).toString());
}
public void printSize() {
System.out.println((new StringBuilder()).append("There are ").append(
size).append(" monsters in the list").toString());
}
If anyone could help that would very much appreciated.
- 12-07-2010, 07:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
You just showed us yourself how to change it; e.g. this:
becomes this:Java Code:System.out.println((new StringBuilder()) .append("The list capacity is ").append(cap).toString());
Or isn't this what you were asking?Java Code:System.out.println("The list capacity is "+cap);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-07-2010, 08:12 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
- 12-07-2010, 08:21 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 12-07-2010, 08:21 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
StringBuilder isn't easier, but it's more efficient. And the way you've used it, you don't gain any efficiency advantages.
Both of these methods will give the same results. The blue version will be more efficient. You're not likely to notice the difference for values of thisMuch less than 10000 or so.Java Code:[COLOR="Red"] public String howMuchLonger(int thisMuch) { String result = "I want to build a "; for (int i = 0; i < thisMuch; i++) { result += "much, "; } result += "much longer String."; return result; } [/COLOR] [COLOR="Blue"] public String howMuchLonger(int thisMuch) { StringBuilder sb = new StringBuilder("I want to build a "); for (int i = 0; i < thisMuch; i++) { sb.append("much, "); } sb.append("much longer String."); return sb.toString(); } [/COLOR]
Both of these methods will give the same results. In this case, the blue version will not be any more efficient than the red. In fact, it will be less efficient, and it's much harder to read and understand.Java Code:[COLOR="Red"] public void printCapacity(int cap) { System.out.println("The list capacity is " + cap + "."); } [/COLOR] [COLOR="Blue"] public void printCapacity(int cap) { System.out.println((new StringBuilder("The list capacity is ")).append(cap).append(".").toString()); } [/COLOR]
Note that in your original post, you're missing a closing ) after cap.
-Gary-
- 12-07-2010, 10:09 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks