-1
Good morning! I made a code to read a char that can be a number or a keyboard letter. It’s very simple and running, but I made an integer n for the size of the vector and put the for i<vector.length, so i goes from 0 to the size of the vector. In this case I type 3 for n and the vector is with 4 elements. In this case I start i with 1, but the error because he read the first char out of the for. Someone could show me where I’m going wrong?
The next phase of the program is to put a while and exit the program when someone type the 'Esc' key, I’m still searching how to use the default asc keyboard, if anyone has any suggestions, thank you.
Follow the tickle:
Scanner sc = new Scanner (System.in);
System.out.print("Digite a quantidade de teclas: ");
int n = sc.nextInt();
char[] teclaon = new char[n];
int sum1 = 0;
int sum2 = 0;
System.out.print("Digite uma tecla: ");
char tecla = sc.next().charAt(0);
for (int i=0; i<teclaon.length; i++) {
if (tecla == '0' || tecla =='1' || tecla =='2' || tecla =='3' || tecla =='4' || tecla =='5'
|| tecla=='6' || tecla =='7' || tecla =='8' || tecla =='9' ){
System.out.println("Você digitou um numero!");
teclaon[i] = tecla;
sum1++;
System.out.print("Digite uma tecla: ");
tecla = sc.next().charAt(0);
}
else {
System.out.println("Voce digitou uma letra!");
teclaon[i] = tecla;
sum2++;
System.out.print("Digite uma tecla: ");
tecla = sc.next().charAt(0);
}
}
System.out.print("Teclas digitadas: [ ");
for (char z : teclaon) {
System.out.print(z + " ");
}
System.out.println("]");
System.out.println("Total de numeros = " + sum1);
System.out.print("Total de letras = " + sum2);
sc.close();
Thanks for the help!
I don’t know if I understand what the problem is, but finally, it might help: https://ideone.com/6IKBTa
– hkotsubo