logo

Scanner e nextChar() in Java

Classe scanner in Java supporta nextInt(), nextLong(), nextDouble() ecc. Ma non c'è nextChar() (Vedi Questo per esempi) Per leggere un carattere, usiamo next().charAt(0) . La funzione next() restituisce il token/parola successivo nell'input come stringa e la funzione charAt(0) restituisce il primo carattere in quella stringa, il numero 0 nella funzione in CharAt( NUMERO ) rappresenta l'indice della singola parola della stringa presa in input e imposta quel carattere di indice sulla variabile char.

Giava








// Java program to read character using Scanner> // class> import> java.util.Scanner;> public> class> ScannerDemo1 {> >public> static> void> main(String [] args){> >Scanner sc =>new> Scanner(System.in);> >char> c = sc.next().charAt(>1>);> >// here this is the indexing of the element to take //> >System.out.println(>'c = '>+c);> >}> > }> > > >

fai while loop java



>

>

Ingresso:

ge>

Produzione :

c = e>