Java program to check if a character is upper or lower case / number / vowel

As I said, how to check if the entered character is one of the parameters? I wrote this code, but it doesn't seem to work very well (or at all), but no errors. In addition, I need to use the base code that I used here. This is for school, and we lose points if we use something that we have not been taught (darn school).

class doody { public static void main(String[] args) { char i; char input='D'; for(i='A';i<='Z';i++)//check if uppercase { if(input==i){ System.out.println("Uppercase"); switch(input){ case 'A': case 'E': case 'I': case 'O': case 'U': System.out.println("Vowel"); break; default: System.out.println("Not a vowel"); break;} } for(i='a';i<='z';i++)//check if lowercase { if(input==i){ System.out.println("Lowercase"); switch(input){ case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("Vowel"); break; default: System.out.println("Not a vowel"); break; }} for(i='0';i<='9';i++)//check if number { if(input==i) System.out.println("Number"); } } }}} 

Edit: Here is the code I compiled today. Much easier. I do not know why this has not happened before. Probably because I was awkward, it was too late.

 class doody { public static void main(String[] args) { char input='$';//input here. boolean lorn=false; if(input>='a'&&input<='z') {System.out.println("Lowercase"); lorn=true; if(input=='a')System.out.println("Vowel."); if(input=='e')System.out.println("Vowel."); if(input=='i')System.out.println("Vowel."); if(input=='o')System.out.println("Vowel."); if(input=='u')System.out.println("Vowel."); } if(input>='A'&&input<='Z') {System.out.println("Uppercase"); lorn=true; if(input=='A')System.out.println("Vowel."); if(input=='E')System.out.println("Vowel."); if(input=='I')System.out.println("Vowel."); if(input=='O')System.out.println("Vowel."); if(input=='U')System.out.println("Vowel."); } if(input>='0'&&input<='9') { lorn=true; System.out.println("Number"); } if(lorn==false)System.out.println("It is a special character"); } } 
+6
source share
8 answers

You do not need a for loop in your code.

Here is how you can implement your method

  • If the input is between β€œA” and β€œZ”, its upper case
  • If the input is between 'a' and 'z', its lowercase
  • If the input is one of 'a, e, i, o, u, A, E, I, O, U' its vowel
  • Elcon consonant

Edit:

Below is a tip to continue. After the code snippet, int values ​​are given for char s

 System.out.println("a="+(int)'a'); System.out.println("z="+(int)'z'); System.out.println("A="+(int)'A'); System.out.println("Z="+(int)'Z'); 

Exit

 a=97 z=122 A=65 Z=90 

Here's how you can check if x exists between two numbers: a and b

 // x greater than or equal to a and x less than or equal to b if ( x >= a && x <= b ) 

During comparisons, char can be considered as numbers

If you can combine these tips, you can find what you want;)

+10
source

If it weren’t homework, you could use existing methods like Character.isDigit(char) , Character.isUpperCase(char) and Character.isLowerCase(char) , which are a little smarter because they don’t work only in ASCII, but also in various encodings.

 static final char[] VOWELS = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; static boolean isVowel(char ch) { for (char vowel : VOWELS) { if (vowel == ch) { return true; } } return false; } static boolean isDigit(char ch) { return ch >= '0' && ch <= '9'; } static boolean isLowerCase(char ch) { return ch >= 'a' && ch <= 'z'; } static boolean isUpperCase(char ch) { return ch >= 'A' && ch <= 'Z'; } 
+9
source

In Java: a character class has a static method called isLowerCase (Char ch) ans isUpperCase (Char ch), Character.isDigit (Char ch) gives you a boolean based on the fact that you can easily achieve your task

example:

String abc = "HomePage";

 char ch = abc.charAt(i); // here i= 1,2,3...... if(Character.isLowerCase(ch)) { // do something : ch is in lower case } if(Character.isUpperCase(ch)) { // do something : ch is in Upper case } if(Character.isDigit(ch)) { // do something : ch is in Number / Digit } 
+4
source

Some comments on your code

  • why do you want to have 2 for loops like for(i='A';i<='Z';i++) , if you can check it with a simple if ... you loop over the whole range, whereas you can just check if it is in this range
  • even when you find your answer (for example, when entering A , you get your result the first time you enter the first cycle), you still do the rest
  • your statement System.out.println("Lowercase"); (and uppercase operator) is placed in the wrong loop
  • If you are allowed to use it, I suggest looking at the Character class, which has, for example, the nice methods isUpperCase and isLowerCase

I will leave everything to you, as this is homework

+2
source

You seem to have upper and lower case in front. AZ will be upper, az will be lower. Although this is not entirely effective - with the exception of the inverted case, I think it should be correctly output.

0
source

This may not be what you are looking for, but I thought you should know the real way to do this. You can use the java.lang.Character isUpperCase() class to find the argument about the character case. You can use isDigit() to distinguish between numbers and letters (it's just FYI :)). Then you can do toUpperCase() and then make a switch for vowels. This will improve the quality of your code.

0
source

This is your homework, so I assume that you NEED to use cycles and switching statuses. This is OK, but why are all your loops internal to your interlocutors?

Just take them to the same level and your code is fine! (part to mixing low / up).

Tip. Pressing additional 'Enter' and 'Space' is free! (This is the first thing I did with your code, and the problem became very trivial)

0
source
  Char input; if (input.matches("^[a-zA-Z]+$")) { if (Character.isLowerCase(input)) { // lowercase } else { // uppercase } if (input.matches("[^aeiouAEIOU]")) { // vowel } else { // consonant } } else if (input.matches("^(0|[1-9][0-9]*)$")) { // number } else { // invalid } 
0
source

Source: https://habr.com/ru/post/906589/


All Articles