Certus
Certus

Reputation: 45

Program to find out whether a character is uppercase, lowercase, or a number

As the title suggests, this program is used to find out whether a number is uppercase, lowercase, or is a number. I'm having some trouble finding out how to write the runner of this program. Any help would be greatly appreciated : ^)

MAIN CLASS:

import static java.lang.System.*;
import java.util.Scanner;

public class characteranalyzer
{
    private char theChar;

    public characteranalyzer(char c)
    {
        theChar = c;
    }

    public void setChar(char c)
    {
        theChar = c;
    }

    public char getChar()
    {
        char getChar = theChar;
        return getChar;
    }

    public boolean isUpper()
    {
        if (((int)theChar)>64&&((int)theChar)<91){
            boolean isUpper = true;
            return isUpper;
        }
        else{
            boolean isUpper = false;
            return isUpper;

        }
    }

    public boolean isLower()
    {
        if (((int)theChar)>96&&((int)theChar)<123){
            boolean isLower= true;
            return isLower;
        }   
        else{
            boolean isLower= false;
            return isLower;
        }
    }

    public boolean isNumber()
    {
        if (((int)theChar)>48 &&((int)theChar+48)<58){
            boolean isNumber = true;
            return isNumber;
        }   
        else{
            boolean isNumber= false;
            return isNumber;
        }

    }   

    public int getASCII( )
    {
        return ((int)theChar);
    }

    public String toString()
    {
         if(isNumber()== true && isUpper() == false && isLower() == false){
                String chartype = "a number";
         return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
            }
            else if(isNumber() == false && isUpper() == true && isLower() == false){
                String chartype = "an uppercase character";
         return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
            }
            else if(isNumber()== false && isUpper() == false && isLower() == true){
                String chartype = "a lowercase character";
         return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
            }
        return null;
        }     
    }

RUNNER CLASS:

import static java.lang.System.*;
    import java.util.Scanner;

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

            out.print("Enter a letter :: ");
            char letter = keyboard.next().charAt(0);
            characteranalyzer test = new characteranalyzer(letter);
            out.println(test);   //A

            out.print("Enter a letter :: ");
            char letter2 = keyboard.next().charAt(0);
            characteranalyzer test2 = new characteranalyzer(letter2);
            out.println(test2);   //l

            out.print("Enter a letter :: ");
            char letter3 = keyboard.next().charAt(0);
            characteranalyzer test3 = new characteranalyzer(letter3);
            out.println(test3);   //a

            out.print("Enter a letter :: ");
            char letter4 = keyboard.next().charAt(0);
            characteranalyzer test4 = new characteranalyzer(letter4);
            out.println(test4);   //7

            out.print("Enter a letter :: ");
            char letter5 = keyboard.next().charAt(0);
            characteranalyzer test5 = new characteranalyzer(letter5);
            out.println(test5);   //D

            out.print("Enter a letter :: ");
            char letter6 = keyboard.next().charAt(0);
            characteranalyzer test6 = new characteranalyzer(letter6);
            out.println(test6);   //2

            out.print("Enter a letter :: ");
            char letter7 = keyboard.next().charAt(0);
            characteranalyzer test7 = new characteranalyzer(letter7);
            out.println(test7);   //X

            out.print("Enter a letter :: ");
            char letter8 = keyboard.next().charAt(0);
            characteranalyzer test8 = new characteranalyzer(letter8);
            out.println(test8);   //Z

            out.print("Enter a letter :: ");
            char letter9 = keyboard.next().charAt(0);
            characteranalyzer test9 = new characteranalyzer(letter9);
            out.println(test9);   //9


        }
    }

Upvotes: 1

Views: 2529

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201437

I suggest you use Character because it has isUpperCase(char), isLowerCase(char) and isDigit(char). You could do also use a loop like,

Scanner keyboard = new Scanner(System.in);
while (true) {
    System.out.println("Please enter a letter :: quit to stop");
    String str = (keyboard.hasNextLine()) ? keyboard.nextLine()
            : "quit";
    if (str.equalsIgnoreCase("quit")) {
        break;
    }
    char ch = str.charAt(0);
    if (Character.isUpperCase(ch)) {
        System.out.printf("%c is uppercase%n", ch);
    } else if (Character.isLowerCase(ch)) {
        System.out.printf("%c is lowercasee%n", ch);
    } else if (Character.isDigit(ch)) {
        System.out.printf("%c is a digit%n", ch);
    } else {
        System.out.printf("%c is not uppercase, lowercase "
                + "or a digit%n", ch);
    }
}

The ? : conditional operator (or ternary) allows you to intialize the String str with "quit" when there is no more input from the Scanner.

Upvotes: 2

Sarthak Mittal
Sarthak Mittal

Reputation: 6104

You can check for ASCII values:

if character has ascii value between 48 and 57(both inclusive) then it is a number.

if it is between 65 and 90 (both inclusive) then it is an Uppercase alphabet.

if it is between 97 and 122 (both inclusive) then it is a Lowercase alphabet

you can refer this link for ASCII table

Upvotes: 2

Related Questions