简体   繁体   中英

java for loop 1st char of 1st string then last char of second string

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);

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.

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()

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.

Try this

Code

    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);

Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM