Emmy
Emmy

Reputation: 27

Java to check if a string is upper case, lower case or both

I'm experiencing an error and I'm trying to find if a string is all upper case, all lower case or both.

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter a word");
    String palabra = sc.next();

    for (int ch = 0; ch < palabra.length(); ch++) {
        char ch = palabra.charAt(i);
        System.out.print(palabra.charAt(ch));

        if (ch >= 'A' && ch <= 'Z') {
            System.out.println("Only upper case");
        } else if (ch >= 'a' && ch <= 'z') {
            System.out.println("Only lower case");
        } else if (ch >= 'a' && ch <= 'z' && ch >= 'A' && ch <= 'Z') {
            System.out.println("Upper case and lower case");
        }
    }
}

Upvotes: 0

Views: 11950

Answers (5)

Stultuske
Stultuske

Reputation: 9437

There is no need to check every character at once. It seems by using the basic methods of the String class, you can seriously simplify your checks:

public boolean isAllUpperCase(String word) {
  if ( word == null ) return false; // this just to avoid NPE
  return word.toUpperCase().equals(word);
}

public boolean isAllLowerCase(String word) {
  if ( word == null ) return false; // this just to avoid NPE
  return word.toLowerCase().equals(word);
}

public boolean isMixedCases(String word) {
  if ( word == null ) return false; // unless you consider null to be mix
  return !isAllLowerCase(word) && !isAllUpperCase(word);
}

Upvotes: 7

Squanch
Squanch

Reputation: 13

Are you able to use the methods from the Character class? Specifically isUpperCase() and isLowerCase()? I like these methods when I tutor since it is pretty straightforward, and gets pupils used to looking into the api for simple solutions. If you can use them, you can add a couple of boolean vars and keep it pretty close to your original:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter a word");
    String palabra = sc.next();
    boolean containsUpper = false;
    boolean containsLower = false;

    char ch;
    for (int i = 0; i < palabra.length(); i++) {
        ch = palabra.charAt(i);
   
        if (Character.isUpperCase(ch)) {
           containsUpper = true;
        } else if (Character.isLowerCase(ch)) {
            containsLower = true;
        }
    }
    if(containsUpper && containsLower){ //If both cases are true.
        System.out.println("Upper case and lower case");
    }
    else if(containsUpper){ //Otherwise, if the upperCase was true,
        System.out.println("All Upper Case");
    }
    else{ // Otherwise the lowerCase was true.
        System.out.println("All Lower Case");
    }
}

The way I've implemented this solution assumes there are only three outcomes: the string contains all upper, all lower, or a mix of both. It doesn't account for symbols other than letters.

Upvotes: 0

Ghost93
Ghost93

Reputation: 181

Here's a possible solution using IntStream:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a word");
        String palabra = sc.next();

        final boolean allUppercase = palabra.chars().allMatch(c -> c >= 'A' && c <= 'Z');
        System.out.println("allUppercase = " + allUppercase);
    }

Similar idea can be used for the other cases.

Upvotes: 0

tgdavies
tgdavies

Reputation: 11421

Here's a sketch of a possible solution, with some blanks for you to fill in.

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter a word");
    String palabra = sc.next();
    boolean mightAllBeUpperCase = true;
    boolean mightAllBeLowerCase = true;
    for (int ch = 0; ch < palabra.length(); ch++) {
        char ch = palabra.charAt(i);
        System.out.print(palabra.charAt(ch));

        if (ch >= 'A' && ch <= 'Z') {
            // what should we do?
        } else if (ch >= 'a' && ch <= 'z') {
            // what should we do?
        } else if (ch >= 'a' && ch <= 'z' && ch >= 'A' && ch <= 'Z') {
            // this can never happen as a single character can't be both!
        }
    }
    // now we've looked at all the letters...
    if (mightBeAllUpperCase) {
       System.out.println("All upper case");
    } else if ( ... what goes here? ) {
       System.out.println("All lower case");
    } else {
       // what's this case for?
    }
}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

Maintain two counters for lowercase and uppercase letters. Then check those counts and print the correct message:

int cntLower = 0;
int cntUpper = 0;

for (int i=0; i < palabra.length(); i++) {
    char ch = palabra.charAt(i);

    if (ch >= 'A' && ch <= 'Z') {
        ++cntUpper;
    }
    else {
        ++cntLower;
    }
}

if (cntLower == 0) {
    System.out.println("Only upper case");
}
else if (cntUpper == 0) {
    System.out.println("Only lower case");
}
else {
    System.out.println("Upper case and lower case");
}

The above assumes that you rely on users only entering A-Z and a-z. Of course, it would be much easier to use String#matches with a regex, e.g.

String palabra = sc.next();
if (palabra.matches("[A-Z]+")) {
    System.out.println("Only upper case");
}
// etc.

Upvotes: 0

Related Questions