Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java How to get scanner.nextLine() on the same line? [duplicate]

Tags:

java

I have a program that takes input from the user from the command line. Annoyingly the first time I use the scanner the inputted text goes on the next line (after the System.out.println). However, all the other times it is on the same line as the System.out.println.

Code:

//Create a scanner
Scanner input = new Scanner(System.in);

//Gets type
System.out.println("--Television Show or Film (t or f): ");
String type = input.nextLine();
input.close();

Output:

--Television Show or Film (t or f): 
t

Desierd Output:

--Television Show or Film (t or f): t

Any help is appreciated. Also, I'm using Eclipse Console.

like image 426
Dan13_ Avatar asked Sep 16 '25 21:09

Dan13_


1 Answers

Use System.out.print rather than System.out.println ?

like image 154
nicomp Avatar answered Sep 19 '25 10:09

nicomp