JavaJoker204
JavaJoker204

Reputation: 83

Java Concatenating two Strings with only charAt

Our task was write a code to concatenate two strings with only using charAt. So what I did was the following:

Scanner sc = new Scanner (System.in);
char operation = sc.nextLine().charAt(0)
String c = "concatenation";
if (operation == 'c' ) {
        System.out.println("Please enter the first string: ");
        String firstString = sc.next();
        System.out.println("Please enter the scond string: ");
        String secondString = sc.next();

        System.out.print("The result of concatenating " + firstString + " and " + secondString + " is ");
        for (int i = 0; i < firstString.length(); i++) {
            char x = firstString.charAt(i);
            System.out.print(x);
        }
        for (int i = 0; i < secondString.length(); i++) {
            char y = secondString.charAt(i);
            System.out.print(y);
        }
    }

My teacher told me the following: "So currently you are simply printing, but the goal is to create a new string, which is the concatenation of both s1 and s2… then you print the new string you created. So using charAt is the way to go, but instead of printing one char at a time, you need to concatenate one char at a time."

So I thought maybe something like this could work:

String concatenated;
    for (int i = 0; i < firstString.length(); i++){
        concatenated = firstString.charAt(i);
    }

Apparently that was not the case, because of incompatible types.

So I wanted to ask you what I could possibly do, to concatenate two strings with only using charAt.

Upvotes: 2

Views: 2485

Answers (2)

Eran
Eran

Reputation: 393831

You are performing assignment, not concatenation, and you can't assign a char to a String variable anyway.

In order to concatenate the characters, you can use the + operator (or +=):

String concatenated = "";
for (int i = 0; i < firstString.length(); i++){
    concatenated += firstString.charAt(i);
}

Or you can use a StringBuilder object and append the characters to it:

StringBuilder concatenated = new StringBuilder(firstString.length()+secondString.length());
for (int i = 0; i < firstString.length(); i++){
    concatenated.append(firstString.charAt(i));
}
for (int i = 0; i < secondString.length(); i++){
    concatenated.append(secondString.charAt(i));
}
System.out.println(concatenated.toString());

This is more efficient than the + operator, since it only generates one StringBuilder object, while the + operator generates a new String object each time you use it.

Upvotes: 3

Andy Turner
Andy Turner

Reputation: 140326

If you want to do it a bit more efficiently than using + or += (which has quadratic time complexity, because you construct a new string on each loop iteration), you can put things in an array:

char[] chars = new char[first.length() + second.length()];
for (int i = 0; i < first.length(); ++i) {
  chars[i] = first.charAt(i);
}
for (int i = 0; i < second.length(); ++i) {
  chars[i + first.length()] = second.charAt(i);
}
String concatenated = new String(chars);

(or you could use a StringBuilder, which is doing basically the same internally).

Upvotes: 2

Related Questions