Why is not entering if?

0

Example:

user type 4A6 then in this string the 2nd position is capital. How do I say that the position the user typed is capital?

Follow the code to clarify

public class JogoMatematica {

public static void main(String[] args) {

    Scanner entrada = new Scanner(System.in);

    System.out.println("Quantidade de caso de teste: ");
    int qtd = entrada.nextInt();

    for (int i = 0; i <  qtd;  i++){ 
        System.out.println("Digite os 3 caracteres: "); 
        // exemplo de entrada 4A6. A letra "A" eh maiuscula
        String caractere = entrada.next(); 
        // pega o elemento da posicao 0
        int valorInt1 = Integer.parseInt(caractere.substring(0,1));
        // pega o elemento da posicao 2
        int valorInt2 = Integer.parseInt(caractere.substring(2)); 
        // pega o elemento na posicao 1
        // se a letra "A" for maiuscula entra no if 
        if(caractere.substring(1,2).toUpperCase().equals(caractere)){ 
             System.out.println("String maiuscula na posicao ");     
        } 

        }
    
asked by anonymous 28.03.2017 / 19:26

1 answer

5

Your code is comparing character 2 with the full string entered by the user, so it will never work:

if(caractere.substring(1,2).toUpperCase().equals(caractere)){ 

What you need to do is just verify that the second character is uppercase, so you can use the Character.isUpperCase method. Your test would look like this:

if (Character.isUpperCase(caractere.charAt(1))) {
  ...
}
    
28.03.2017 / 19:36