Results 1 to 6 of 6
  1. #1
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    You just showed us yourself how to change it; e.g. this:

    Java Code:
    System.out.println((new StringBuilder())
    .append("The list capacity is ").append(cap).toString());
    becomes this:

    Java Code:
    System.out.println("The list capacity is "+cap);
    Or isn't this what you were asking?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    becomes this:

    Java Code:
    System.out.println("The list capacity is "+cap);
    Or isn't this what you were asking?

    kind regards,

    Jos
    yea, but when I write that code it gives me an error for "cap"

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by arri3oi View Post
    yea, but when I write that code it gives me an error for "cap"
    That's an entirely different problem. It probably is an undefined symbol? Show us the code preferably within [code] ... [/code] tags.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    StringBuilder isn't easier, but it's more efficient. And the way you've used it, you don't gain any efficiency advantages.

    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. 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 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]
    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.

    Note that in your original post, you're missing a closing ) after cap.

    -Gary-

  6. #6
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Sorry Jos still a noob.

    Gary, thanks. ")" thats why it was given me that error.

    Thanks guys

Posting Permissions

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