I’m having a problem with reading a string variable in java

Asked

Viewed 59 times

1

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

code snippet:

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

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 the reading of strings with reading primitive numeric types with this class, due to the line break leak.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.