• Register
0 votes
24 views


I wrote a program like this 


 Scanner in = new Scanner(System.in);
   System.out.println("Input a letter"); 
   String line = in.nextLine();
   char c = line.charAt(0);
   if( character.isDigit(c))
   System.out.println(c +" Is a digit");
   else if (character.isLetter(c))
   System.out.println(c +" Is a Letter");

And It gives me error.

How can I solve this problem?

10 7 2
6,060 points

1 Answer

0 votes

In your program you declared Character as character .
You should have programmed like below:

if (Character.isdigit(c)){
 System.out.println(c +" Is a digit");
}
else if (Character.isdigit(c)){
 System.out.println(c +" Is a digit");
}

You can also try this:

     System.out.println("Enter the value");
        String data;
        data = in.next();

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

            if ((ch >= '0' && ch <= '9')){
                System.out.println("Number ");
            }
            else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
            System.out.println("Character");
            }
            else{
                System.out.println("Symbol");
             }
        }

 

11 6 4
34,950 points

Related questions

1 vote
1 answer 145 views
145 views
Problem: I am looking for a solution to my existing code. Well, I am a new student of java. I was thinking about a program that can check whether the given char is an upper case or not! Let&rsquo;s say I&rsquo;ve given a char as &lsquo;a&rsquo; and I want ... , my question is, how to check whether a char variable ch is an uppercase letter, you write? Thanks for your valuable time and effort guys.
asked Mar 22 Gavin 15.3k points
0 votes
1 answer 31 views
31 views
I am very new at java programing, I have an assginment where I have to check if a character is uppercase in java and I wrote a code: public static void main(String[] args) { int A; char a = A; if (Character.isUpperCase()) { System.out.println(a + " is an uppercase character."); } } But it doesn&rsquo;t work how. Can anyone help me out?
asked Mar 15 LizzyM 6.1k points
0 votes
1 answer 18 views
18 views
I was doing my java programming practice. I wrote a code to check if a charcter is a number: public static void main(String[] args) { String string = "-1234.15"; String numeric ; numeric = string.matches("-?\\d+(\\.\\d+)?"); if(numeric) System.out.println(string + " is a number"); else System.out.println(string + " is not a number"); } But it gives me error. Whats wrong in my code?
asked Mar 15 LizzyM 6.1k points