Search code examples
javauppercaselowercasedigit

Char Tester in java (uppercase A-Z,lowercase a-z,digit 0-9,other)


I'm trying to make a program that tells me weather the char i put in arguments is uppercase or lowercase or a digit from 0 to 9 or other! I'm having errors in my code:

public class CharsTester {


    public static void main(String[] args) {


         char input;

         if (input.matches("^[a-zA-Z]+$")) 
         {
             if (Character.isLowerCase(input))
             {
                System.out.println("lower");
             } 
             else
             { 
                 System.out.println("upper");
             }
         }
         else if (input.matches("^(0|[1-9][0-9]*)$"))
         {
             System.out.println("digit");
         }
         else
         {
             System.out.println("other");
         }
    }
}

Solution

  • Try:

    for (String arg : args) {
        if (arg.matches("^[A-Z]+$")) {
            System.out.println("uppercase");
        } else if (arg.matches("^[a-z]+$")) {
            System.out.println("lowercase");
        } else if (arg.matches("^[0-9]+$")) {
            System.out.println("digits");
        } else {
            System.out.println("other");
        }
    }