简体   繁体   中英

Do-while loop “string index out of range”

I'm getting that error and I'm not sure why. I've tried a few different things like starting with (!(flag... then == '+' and one with == to a which is where the line right below the do statement also gets an error as well. Anyone see the problem? The main goal I'm trying to get right now is the for loops to repeat to print the rope again with the flag in a different place left or right.

package program2;

import java.util.Scanner;
import java.lang.Math;

public class Program2 {

public static int MAX_LENGTH = 21;
public static int MIN_LENGTH = 5;

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter the length of the rope: ");
    int ropeLength = keyboard.nextInt();
    while (ropeLength < MIN_LENGTH || ropeLength > MAX_LENGTH || ropeLength % 2 != 1) {
        System.out.println("Thats not a valid length (odd number between 5 and 21)");
        System.out.print("Enter the length of the rope: ");
        ropeLength = keyboard.nextInt();
    }
    char a;
    String flag = "+";
    for (int i = 0; i < ropeLength / 2; i += 1) {
        System.out.print("-");
    }
    System.out.print(flag);

    for (int i = 0; i < ropeLength / 2; i += 1) {
        System.out.print("-");
    }
    System.out.println("");


    do {
        //a = flag.charAt(ropeLength);
        double rand = Math.random();

        if (rand > 0.5) {
            for (int i = 0; i < (ropeLength / 2) - 1; i++) {
                System.out.print("-");
            }

            System.out.print(flag);

            for (int i = 0; i < (ropeLength / 2) + 1; i++) {
                System.out.print("-");
            }
        if (rand < 0.5) {
            for (int i = 0; i < (ropeLength / 2) + 1; i++) {
                System.out.print("-");
                }

            System.out.print(flag);

            for (int i = 0; i < (ropeLength / 2) - 1; i++) {
                System.out.print("-");
               }

            } 
        }
    } while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+');

}

}

and as for the do while loop, my for loops seem to only be repeating once or twice.

 do {
    //a = flag.charAt(ropeLength);
    double rand = Math.random();

    if (rand > 0.5) {
        for (int i = 0; i < (ropeLength / 2) - 1; i++) {
            System.out.print("-");
        }

        System.out.print(flag);

        for (int i = 0; i < (ropeLength / 2) + 1; i++) {
            System.out.print("-");
        }
    if (rand < 0.5) {
        for (int i = 0; i < (ropeLength / 2) + 1; i++) {
            System.out.print("-");
            }

        System.out.print(flag);

        for (int i = 0; i < (ropeLength / 2) - 1; i++) {
            System.out.print("-");
           }

        } 
    }
} while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+');

}

and one final thing, do i need that code that i have commented out right under the do?

You have:

String flag = "+";

and you never modify it. So when you have the condition:

flag.charAt(ropeLength - 1) != '+'

except if ropeLength equals 1, it will always be out of range.

About the actual behavior of your code, as I mentioned, you never modify the flag variable. So its first character will always be '+' and therefore your loop will always be executed once.

So the first issue I see with your code according to your goal is the way you use flag and the print/println method. If you want to know where the '+' is, you could use a StringBuilder this way.

Before:

String flag = "+";
for (int i = 0; i < ropeLength / 2; i += 1) {
    System.out.print("-");
}
System.out.print(flag);

for (int i = 0; i < ropeLength / 2; i += 1) {
    System.out.print("-");
}
System.out.println("");

After:

StringBuilder flag = new StringBuilder( ropeLength );
for (int i = 0; i < ropeLength / 2; i += 1) {
    flag.append( '-' );
}
flag.append( '+' );

for (int i = 0; i < ropeLength / 2; i += 1) {
    flag.append( '-' );
}

System.out.println( flag.toString() );

// Resetting the StringBuilder for reuse
flag.setLength( 0 );

However, about the do/while loop, you should revise your whole a algorithm. The way this is written, if you use StringBuilder as I mentioned above, the '+' will only jitter around the center of the "rope" indefinitely. I'm pretty sure that is not the actual intent.

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