2

I have to make a program that add Strings to each other as many as argument in method says. Program is not working and i dont know why. Thanks for your time. The problem is in the main in ap.append(" ma kota", 3).append( " i psa", 2);. I cant make changes to the main.

public class Appender {
    private StringBuffer sb;

    public Appender(){
        sb = new StringBuffer();
    }

    public Appender(String s){
        sb = new StringBuffer(s);
    }

    public StringBuffer append(String str, int n) {
        while(n > 0){
            sb.append(str);
            n--;
        }
        return sb;
    }

    public String toString(){
        String s = sb.toString();
        return "" + s;
    }
}

And this is my main :

public class Main {
    public static void main(String[] args) {
        Appender ap = new Appender("Ala");
        ap.append(" ma kota", 3).append( " i psa", 2);
        System.out.println(ap);
        ap.append(" ojej", 3);
        System.out.println(ap);
    }
}
4
  • 3
    what kind of error do you get? Commented Apr 17, 2014 at 9:25
  • do you understand this right: you have to append str n-times to your Appender ? so as example: ap.append("hi", 3) will make "hihihi" ? Commented Apr 17, 2014 at 9:27
  • you get print only "Ala" ? right ? Commented Apr 17, 2014 at 9:38
  • Aside: unless you have a specific reason to use StringBuffer you should probably be using StringBuilder instead. Commented Apr 17, 2014 at 10:08

4 Answers 4

2

The append(String appended, int n) method belongs to Appender, not StringBuilder so you have to return it, instead of the sb

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
         Appender ap = new Appender("Ala");
        ap.append(" ma kota", 3).append( " i psa", 2);
        System.out.println(ap);
        ap.append(" ojej", 3);
        System.out.println(ap);
     }
     public static class Appender {
        private StringBuffer sb;

        public Appender(){
            sb = new StringBuffer();
        }

        public Appender(String s){
            sb = new StringBuffer(s);
        }

        public Appender append(String str, int n) {
            while(n > 0){
                sb.append(str);
                n--;
            }
            return this;
        }

        public String toString(){
            return sb.toString();
        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
public StringBuffer append(String str, int n) {
    while(n > 0){
        sb.append(str);
        n--;
    }
    return sb;
}

should change to

public Appender append(String str, int n) {
    while(n > 0){
        sb.append(str);
        n--;
    }
    return this;
}

because StringBuffer does not have method append(String s, int n)

Comments

0

You should return this(current object) from com.test.FunPrime.Appender.append(String, int)

like

public Appender append(String str, int n) {
        while(n > 0){
            sb.append(str);
            n--;
        }
        return this;
    }

Comments

-1

You can also use org.apache.commons.lang.StringUtils#repeat for this.

For example repeat for 5 times:

String result = StringUtils.repeat("repeat this string", ", ", 5);

The version without the second argument (the separator ", ") is also available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.