0

Java - Check if string all upper or lower case letters.

Hi all! Trying to write a small code to check if a string is all upper or lower case letters, such as "JAVA" or "hello", which should return true. If has mixed upper or lower case letters like "SoloLEarn" should return false. I think something is wrong with my logic; I've tried a few different things, like "!" on both conditions or "&&" operator, but my code fails some test cases depending on what I'm trying. Can anyone help? Thanks so much! public static boolean sameCase(String str) { for (char c : str.toCharArray()){ if (!(Character.isUpperCase(c)) || (Character.isLowerCase(c))) return false; } return true; } Here it fails: @Test public void test4() { assertEquals(true, Challenge.sameCase("marmalade")); and @Test public void test7() { assertEquals(true, Challenge.sameCase("pickle")); }

21st Jul 2020, 2:17 PM
GG128
10 odpowiedzi
+ 4
ChaoticDawg Yes, your right. That should be the most effective way to do it. 👍 I completely forgot that strings are immutable so you don't need an extra copy.
21st Jul 2020, 5:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
The result of your method will always be false unless you don't use an empty string. It is just working completely wrong. You check for each character if it is upper case OR lower case. This is always true and due to that you always return false.
21st Jul 2020, 2:26 PM
Sandra Meyer
Sandra Meyer - avatar
+ 3
that condition will always return False. one way to do it is : check the first char in str and see if it is lower or uppercase then check the rest based on the first status. if they are all the same case return True otherwise false.
21st Jul 2020, 2:28 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 3
Hello GE12 !isUpperCase() = isLowerCase() I am not sure what is returned for special characters but normally if a letter is not upper case it is lower case. And I think you need a letter to compare the others. I would take the first letter, check if it is upper or lower case (you cN store it in a boolean). Then check the other letters if they are also upper or lower case.
21st Jul 2020, 2:37 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
isUpperCase() will return true for characters from 'A'-'Z' anything else will return false. Likewise, isLowerCase() will return true for characters from 'a'-'z' anything else will return false. This code will find the first letter in the string and see if it is uppercase then compare all other letters in the string to check if they are the same case. If not it returns false. Otherwise, true. "sololearn" = true "SoloLearn" = false "@sololearn!" = true "@SoloLearn!" = false Etc. private static boolean sameCase(String str) { char[] chars = str.toCharArray(); boolean isSame = false; for(char ch: chars) { if(Character.isLetter(ch)) { isSame = Character.isUpperCase(ch); break; } } for(char ch: chars) { if(Character.isLetter(ch) && Character.isUpperCase(ch) != isSame) return false; } return true; }
21st Jul 2020, 5:12 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
What also should work: Create a new string which copies your first string. Then compare both strings: if(str.equals(copy.toLowerCase()) || str.equals(copy.toUpperCase()) return true; else return false;
21st Jul 2020, 5:34 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
all you really need is return str.equals(str.toUpperCase()) || str.equals(str.toLowerCase())
21st Jul 2020, 5:37 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
GE12 Ya change the keyword private to public or remove it completely depending on your code. Same with static if it isn't needed, remove it.
21st Jul 2020, 6:55 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks all so much for your responses! This is very informational : ) Nice code @codemonkey. I think it's very good for a beginner. @ChaoticDawg, thanks so much for the code sample! Makes sense!
21st Jul 2020, 6:51 PM
GG128
+ 1
Check with ASCII code (97-122) for lowercase alphabet and 65-90 for uppercase alphabet by extracting a single character from string.
23rd Jul 2020, 12:35 PM
shubham kumar
shubham kumar - avatar