Reputation: 59
when i input two strings of equal length, my code works just fine, but when i do it like different length of strings, it says StringIndexOutOfBoundsException.
here is my code... i need to output a5b4c3d21 this is not homework, i am just studying String manipulation through for loop. thank you in advanced.
String name1 = "abcd";
String name2 = "12345";
String temp = "";
for (int i = 1; i <= name1.length() || i <= name2.length(); i++) {
temp = temp + name1.charAt(i-1) + name2.charAt(name2.length()-i);
}
System.out.println(temp);
Upvotes: 1
Views: 84
Reputation: 2188
Try this
String name1 = "abcd";
String name2 = "12345";
String temp = "";
int i;
for (i = 0; i < name1.length() && i < name2.length(); i++) {
temp = temp + name1.charAt(i) + name2.charAt(name2.length()-i-1);
}
for (; i < name1.length(); i++) {
temp = temp + name1.charAt(i);
}
for (; i < name2.length(); i++) {
temp = temp + name2.charAt(name2.length()-i-1);
}
System.out.println(temp);
a5b4c3d21
2 loops after the first loop is to continue when one of the Strings have greater length. First loop terminates when one of the String's end is reached
Upvotes: 0
Reputation: 201497
I would use Math.max(int,int)
to get the larger String
length. And iterate from 0 to the length of the larger String
. Test if each position is less then the last character of the String name1
if so print that character. Then test name2
. Like
String name1 = "abcd";
String name2 = "12345";
for (int i = 0, len = Math.max(name1.length(), name2.length()); i < len; i++) {
if (i < name1.length()) {
System.out.print(name1.charAt(i));
}
if (i < name2.length()) {
System.out.print(name2.charAt(name2.length() - i - 1));
}
}
System.out.println();
Output is (as requested)
a5b4c3d21
We might simplify the math in the second if
by using a StringBuilder
and reverse()
like
String name1 = "abcd";
String name2 = "12345";
StringBuilder sb = new StringBuilder(name2);
sb.reverse();
for (int i = 0, len = Math.max(name1.length(), sb.length()); i < len; i++) {
if (i < name1.length()) {
System.out.print(name1.charAt(i));
}
if (i < sb.length()) {
System.out.print(sb.charAt(i));
}
}
System.out.println();
And the output is the same.
Upvotes: 0
Reputation: 101738
You need to do bounds checking:
for (int i = 1; i <= name1.length() || i <= name2.length(); i++) {
if (i <= name1.length()) {
temp += name1.charAt(i - 1);
}
if (i <= name2.length()) {
temp += name2.charAt(name2.length() - i);
}
}
You could use the conditional operator to make it a bit less verbose:
for (int i = 1; i <= name1.length() || i <= name2.length(); i++) {
temp += (i <= name1.length() ? name1.charAt(i - 1) : "") +
(i <= name2.length() ? name2.charAt(name2.length() - i) : "");
}
but I recommend the first version, which is clearer.
Upvotes: 1
Reputation: 13596
You have a logic error here:
i <= name1.length() || i <= name2.length()
You're going up to the end of the longer string. You keep going if either string still has letters, so you stop when both strings are finished. But if the strings are different lengths, you keep going even after one string is passed.
Try:
i <= name1.length() && i <= name2.length()
Upvotes: 0