1

I was wondering if there's a way to do use String::concat with loops, instead of using StringBuilder/StringBuffer.

I tried it like this, but it doesn't work. Could someone help fix up the problem? or give me suggestions so I try it myself?

    public class multiConcat {
    public static void main(String[] args) {
        int num = 0;
        String finish = "";
        Scanner reader = new Scanner(System.in);

        System.out.println("Type a word: ");
        String state = reader.next();
        System.out.println("Number of Concatenation: ");
        num = reader.nextInt();
        finish = state.concat(state);

            for (int i = 0; i == num; i++) {
              finish.concat(state);
            }

        System.out.println(finish);

    }
}

I thought it would be sthe ame idea as x = x + 1, i.e. constantly overwriting the value...

Thank you

you mean as this?

        for (int i = 0; i == num; i++) {
            finish = finish.concat(state);
        }
3
  • Could you post the error you are getting? Also, the first phrase is somewhat confuse, consider making it clearer. Commented Nov 21, 2014 at 4:43
  • I'm not getting any compiling errors. What im trying to do is if I enter "hi" for my word and i wanted to concat my world 4 times, so the result of this would be like "hihihihi" sorry for bad grammer Commented Nov 21, 2014 at 4:45
  • concat doesn't modify the string. use finish = finish.concat(state);. and remove this line finish = state.concat(state); Commented Nov 21, 2014 at 5:08

3 Answers 3

2

finish.concat(state) returns a new String, so you have to assign in to the finish variable in order for the reference to the new String to be kept :

finish = finish.concat(state);

You already do it prior to the loop, but inside the loop you are not doing anything with the return value of concat, and therefore the loop changes nothing.

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

1 Comment

@JasonKim you didn't fix the problem in the for loop, which I meantioned in my answer.
0

Few Changes.

1.) looping was not correct.

should be as below, if num > 1 then concatenate in loop.

 if (num > 1) {
            for (int i = 0 ; i < num ; i++) {
                finish = finish.concat(state);
            }
        } else {
            finish = finish.concat(state);
        }

2.) finish.concat(state) returns a new String, so you have to assign in to the finish variable

finish = finish.concat(state); // **remember Strings are immutable**

Complete program

public class multiConcat {
    public static void main(String[] args) {
        int num = 0;
        String finish = "";
        Scanner reader = new Scanner(System.in);

        System.out.println("Type a word: ");
        String state = reader.next();
        System.out.println("Number of Concatenation: ");
        num = reader.nextInt();

        if (num > 1) {
            for (int i = 0 ; i < num ; i++) {
                finish = finish.concat(state);
            }
        } else {
            finish = finish.concat(state);
        }

        System.out.println(finish);

    }
}

output

Type a word: 
ankur
Number of Concatenation: 
2
ankurankur


Type a word: 
ankur
Number of Concatenation: 
1
ankur

Type a word: 
ankur
Number of Concatenation: 
3
ankurankurankur

6 Comments

ignore the if statement sorry... i posted the wrong code.. I was trying something on my own
I was trying to make it so If the person wants it concatenated less than twice It will just output the word
@JasonKim ok, that you can put a filter for <2, those things you can tweek i hope.
@JasonKim please upvote/accept the answer if it solves your purpose
I have couple more question
|
0

You can use below code.

String is immutable so you have to store returned value.

String finish = new String();
finish = finish.concat("Any Value");

Using StringBuilder or StringBuffer you can do this way

StringBuilder finish = new StringBuilder();
finish.append(state);

StringBuffer finish = new StringBuffer();
finish.append(state);

2 Comments

Thank you for the answer. I will try using Stringbuilder and Stringbuffer next time :D
No problem. But 1 advice, if you are changing string value then it is better to use StringBuilder or Stringbuffer. Because everytime concat is called new memory is allocated for that string, as String is immutable.

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.