0

I have an argument which is of type char. I want to check that this char is lower case, if this is true then I will make a boolean variable equal true, otherwise, make it equal false. I have created an array of chars:

String argumentStr = args[2];
char argument = argumentStr.charAt(0);
boolean acceptArgument;

char[] lowerCaseAlphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

Then I have tried two different solutions, but each is outside of the scope of my acceptArgument boolean.

First:

for (int i = 0; i < 27; i++) {
        if (argument == lowerCaseAlphabet[i]) {
            acceptArgument = true;
        } else {
            acceptArgument = false;
        }
    }

Second:

for (char letter: lowerCaseAlphabet) {
        if (argument == letter) {
            acceptArgument = true;
        } else {
            acceptArgument = false;
        }
    }

I understand why it won't work, because of the scope of the if statements compared with the acceptArgument boolean. But I don't know how to get around this. Please advise.

2
  • add break; after acceptArgument = true; Commented Sep 19, 2018 at 16:49
  • No, that's not a scope problem. Use your debugger, or add println() statements inside the if and inside the else, and you should understand why. Commented Sep 19, 2018 at 16:49

2 Answers 2

1

You don't need an extra array for this. You can check if argument is lower case by comparing the result to Character.toLowerCase(char) like

char argument = argumentStr.charAt(0);
boolean acceptArgument = argument == Character.toLowerCase(argument);

or (as pointed out by @JBNizet) use Character.isLowerCase(char) like

boolean acceptArgument = Character.isLowerCase(argument);

If you also need to test that the character is a letter you can add an and for that like

boolean acceptArgument = Character.isLetter(argument) 
        && Character.isLowerCase(argument);
Sign up to request clarification or add additional context in comments.

4 Comments

Why not just call isLowerCase()?
@JBNizet Or that indeed. :)
Strictly speaking, it is not equivalent, "э" is lower case but not in a-z.
@ElliottFrisch I should also mention that I need the argument to only be letters, it can't be numbers or special characters. What would I do in this instance?
0

I understand why it won't work, because of the scope of the if statements compared with the acceptArgument boolean

No. the reason is not scope, it is because of at each iteration acceptArgument is getting either false or truebased on if condition until it finishs the whole loop because you did not use any break or what ever to get out of the loop when match found.

For example if argument = 'p', it will find match at letter='p', and therefore acceptArgument will be set to true. However, the loop advances to next letter q, sincep != q, acceptArgument will be set to false and continues like that. that is why it is not working what you expect.

see the correction below

to correct your code, make the following modification

First initialize;

boolean acceptArgument=false;

Second remove else it should be like this

for (char letter: lowerCaseAlphabet) {
    if (argument == letter) 
        acceptArgument = true;          
}

it works.

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.