10

As I said before, how do I test if the entered character is one of the parameters? I've written this code, but it doesn't seem to run very well(or at all), no errors, however. Also, I need to use the basic code I've used here. Its for school and we lose points if we use things they haven't taught us (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 some code I threw together today. Much simpler. I don't know why this didn't occur to me earlier. It was probably because I was groggy, it was 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");
 }
}
durron597
  • 31,968
  • 17
  • 99
  • 158
Prnth
  • 143
  • 1
  • 1
  • 9
  • 2
    You should try to solve one part of this assignment at a time rather than all at once. Break it down into smaller parts and solve each small question. Then if you get stuck at a specific sub point, we'll be better able to help you. **Edit**: where does the user enter the character?? – Hovercraft Full Of Eels Jan 22 '12 at 15:12
  • 3
    You are confusing uppercase and lowercase. Uppercase is A-Z, lowercase is a-z. – Arjan Jan 22 '12 at 15:13
  • 2
    Your code is very hard to read because it's not indented correctly. Only put one `}` per line. – JB Nizet Jan 22 '12 at 15:16
  • Thanks for pointing out the uppercase and lowercase issue. I kinda switched the two (I have no idea why) and I might've missed that. – Prnth Jan 22 '12 at 15:36
  • @HovercraftFullOfEels They haven't taught us any input methods yet, so I have to type the character in manually. I assigned it to the char called "input". – Prnth Jan 22 '12 at 15:41
  • "it doesn't seem to run very well(or at all)" as a description of the problem, that is almost worthless – Raedwald Jul 09 '13 at 21:12

8 Answers8

13

If it weren't a homework, you could use existing methods such as Character.isDigit(char), Character.isUpperCase(char) and Character.isLowerCase(char) which are a bit "smarter", because they don't operate only in ASCII, but also in various charsets.

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';
}
Natix
  • 14,017
  • 7
  • 54
  • 69
12

You don't need a for loop in your code.

Here is how you can re implement your method

  • If input is between 'A' and 'Z' its uppercase
  • If input is between 'a' and 'z' its lowercase
  • If input is one of 'a,e,i,o,u,A,E,I,O,U' its Vowel
  • Else Consonant

Edit:

Here is hint for you to proceed, Following code snippet gives int values for chars

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

Output

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

Here is how you can check if a number x exists between two numbers say 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 chars can be treated as numbers

If you can combine these hints, you should be able to find what you want ;)

Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
  • How do I test if it is between A and Z? I'm a Java noob, you see. – Prnth Jan 22 '12 at 15:37
  • You already do it: `for(i='A';i<='Z'`. `i` starts with `A`, and the loop continues while i is lower than (or equal to) `Z`. – JB Nizet Jan 22 '12 at 15:45
  • @JB I can understand why you wouldn't want to confuse him and keep with his approach, but I think we should tell beginners their obvious problems and give them better solutions. The [for-if anti pattern](http://blogs.msdn.com/b/oldnewthing/archive/2011/12/27/10251210.aspx) isn't something even beginners should get used to. – Voo Jan 22 '12 at 19:54
  • This isn't my point. He asked how to test if a char is between A and Z, and I told him that he already did that in his original code, since its loop uses such a comparison already. – JB Nizet Jan 22 '12 at 19:59
7

In Java : Character class has static method called isLowerCase(Char ch) ans isUpperCase(Char ch) , Character.isDigit(Char ch)gives you Boolean value, base on 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
}
Chetan Bhoyar
  • 501
  • 4
  • 12
2

Some comments on your code

  • why would you want to have 2 for loops like for(i='A';i<='Z';i++), if you can check this with a simple if statement ... you loop over a whole range while you can simply check whether it is contained in that range
  • even when you found your answer (for example when input is A you will have your result the first time you enter the first loop) you still loop over all the rest
  • your System.out.println("Lowercase"); statement (and the uppercase statement) are placed in the wrong loop
  • If you are allowed to use it, I suggest to look at the Character class which has for example nice isUpperCase and isLowerCase methods

I leave the rest up to you since it is homework

Robin
  • 36,233
  • 5
  • 47
  • 99
  • How do I test if it is within a range? Do I need to use one of those...array things? Thanks for the "System out in the wrong loop" thing. I'll fix that now. – Prnth Jan 22 '12 at 15:38
0
 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
 }
d.moncada
  • 16,900
  • 5
  • 53
  • 82
0

You appear to have upper and lowercase back to front. A-Z would be upper, a-z would be lower. While not exactly efficient - with the inverted case exception, I think it should output correctly.

Dan Hardiker
  • 3,013
  • 16
  • 19
0

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

MozenRath
  • 9,652
  • 13
  • 61
  • 104
  • I know about that, but we haven't been taught it in school. We have to stick to what the teacher has taught us. I lost heaps of marks in the exams for writing things the teacher hadn't taught us. – Prnth Jan 22 '12 at 15:44
  • then i think you should use 2 separate booleans to set the properties of uppercase/lowercase and consonant/vowels then do a print based on those booleans – MozenRath Jan 22 '12 at 15:51
0

This is your homework, so I assume you NEED to use the loops and switch statments. That's O.K, but why are all your loops inner to the privious ones ?

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

A tip: Pressing extra 'Enter' & 'Space' is free! (That's the first thing I did to your code, and the problem became very trivial)

222
  • 1,257
  • 2
  • 8
  • 4
  • Great idea, good sir. I'm going to bed now, its late here, but I will work on it as soon as I get back from school tomorrow, and put an updated version here. – Prnth Jan 22 '12 at 16:02