0
char[] message = input.toCharArray ();

String x = "";
String encrypted2 = "";

for (int index = 0 ; index < message.length ; index++)
{
    message [index]++;
    message [index]++;
}

String encrypted = new String (message);

I need the for loop to run only if the char array contains letters from A to N.

1
  • Make a boolean indicator. Iterate the array and check each char. If any of them are not the ones you want, make the indicator false. Check the indicator before running your loop. Commented Dec 13, 2017 at 22:05

1 Answer 1

1

You need a second loop before the loop in your code:

boolean cond = true;
for(int i = 0; cond && i < message.length; i++) {
    if(message[i] < 'A' || message[i] > 'N')
        cond = false;
}
for (int index = 0 ; cond && index < message.length ; index++)
{
    message [index]++;
    message [index]++;
}

The first loop goes through and checks if all the characters are A through N, and upon finding a character that's not, stops checking the rest of the array and doesn't execute the loop.

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

Comments

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.