logo

Skener a nextChar() v jazyku Java

Trieda skenera v jazyku Java podporuje nextInt(), nextLong(), nextDouble() atď. Ale neexistuje nextChar() (Pozri toto pre príklady) Na čítanie znaku používame next().charAt(0) . funkcia next() vráti ďalší token/slovo na vstupe ako reťazec a funkcia charAt(0) vráti prvý znak v tomto reťazci, číslo 0 vo funkcii v CharAt( NUMBER ) predstavuje index jedného slova zo vstupného reťazca a nastavte tento znak indexu na premennú char.

Java








// 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);> >}> > }> > > >



>

>

Vstup:

ge>

Výkon :

c = e>