0

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?

2
  • Assume ropeLength = 13. What should the value of flag.charAt(ropeLength - 1) be? Commented Mar 16, 2013 at 0:53
  • You flag variable is of length 1. Commented Mar 16, 2013 at 1:00

1 Answer 1

1

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.

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

3 Comments

So, I delete my answer, the same as yours.
for the flag.charAt(ropeLength) != '+' part, doesnt the notequals just check to see if the character there is a +?
hmm well i fixed the while, but its only repeating twice no matter what then ending.

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.