I'm having a problem reading a string variable in java

1

I created a variable of type string and it is not reading a sentence with a line break, it is simply jumping, in java is there a way to clear buff like C or something that can do?

code snippet:

Scanner sc = new Scanner(System.in);
String nome;
nome = sc.next();
    
asked by anonymous 14.06.2018 / 14:08

1 answer

7

To read with line break you have to use nextLine() :

Scanner sc = new Scanner(System.in);
String nome;
nome = sc.nextLine();

Just be careful when mixing Strings reading with numeric primitive types with this class, due to casting of the line break .

    
14.06.2018 / 14:09