How to use the Scanner to capture input from the keyboard? [duplicate]

6

I would like to know how to use Scanner in java to simulate cin and cout of c ++

    
asked by anonymous 17.02.2016 / 23:26

1 answer

9

Using Scanner you can capture data entry using the methods below, which are the most common:

Scanner s = new Scanner(System.in);

s.nextInt();
s.nextDouble(); 
s.nextFloat(); 
s.next(); //captura como string mas se o trecho tiver espaço, ignora o que
          //estiver após o espaço. Não move o scanner pra linha seguinte
s.nextLine();// captura como string uma linha inteira e para após uma quebra
             //de linha, move o scanner para a linha seguinte

You only have to be careful when using captures of primitive types with String captures, because of the line break .

And for data output, java has System.out.println() which displays information by inserting a line break, and System.out.print() does the same thing, but without the line break.

    
17.02.2016 / 23:37