How do I make a comparison of a character in the range of an alphabet?

-4

The question is:
 1) Help the university to assemble the divisions of the programming lab. To do this, write an algorithm that reads the name of the student and say in which division he is respecting the rule below:
-children whose name begins with the letters from A to K are in D1 ;
- Students whose name begins with the letters of L and N are in D2 ;
- Students whose name begins with the letters from O to Z are in D3 . Tip: Use the charAt(posição) method?

 public class ex4a3{
    public static void main (String[] args){
       String nome = JOptionPane.showInputDialog("Digite seu nome para
descobrir sua divisao:");
   char letra = nome.charAt(0); 

   if(letra >= "a" && letra <= "l"){
     JOptionPane.showMessageDialog(null,"Voce esta na divisao D1");
   }

   }
}
    
asked by anonymous 21.03.2017 / 01:56

1 answer

1

This question is about the types you are using.

"a" (note the double quotes, the " character) is String .

'a' (note the apostrophe / single quotes, the ' character) is char .

Also note that 'A' is different from 'a' . The basic characters (without accents, cedillas or other differential marks) have the numbering defined by the table ASCII .

Another alternative is to use regular expressions. Due to how you wrote the question, it seems like you are learning programming in Java, so you may not feel very comfortable with regular expressions. On Wikipedia you have an example of how to write regular expressions .

In the example below, you can play around with the values of the nome variable to know what matches a pattern ( Pattern ). In case, the pattern I created fits in with anything starting with the letters from a to k , ignoring whether it's in the upper or lower case.

import totalcross.util.regex.Pattern;

public class Teste {

    public static void main(String[] args) {
        String nome = "luiz";
        Pattern pattern = new Pattern("^[a-k].*", "i");
        if (pattern.matches(nome)) {
            System.out.println("match");
        } else {
            System.out.println("not match");
        }
    }

}
    
22.03.2017 / 19:21