0

Is there a way to print out a string on the same line after the user has input their name and hit enter? Not sure if its possible with scanner so any input on other solutions is helpful.

Example.

Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.nextLine();
System.out.print("   Hello" + name);

Current Console Output:

Enter your name:
Dylan
   Hello Dylan

Desired Console Output:

Enter your name:
Dylan   Hello Dylan

I have tried changing print to println and vice versa but it seems to me like .nextLine() or something else with the scanner class itself is inserting a new line operator.

Dylanrr
  • 31
  • 1
  • 6
  • it looks like scanner requires newline in order to read. you may be able to print a backspace but apparently doesn't always work. – mavriksc Feb 06 '19 at 21:57
  • you can not override Scanner because it is final. – Alex Feb 06 '19 at 22:19
  • That's what I was thinking if anyone has suggestions on other input options that do not need a newline i'm all ears. – Dylanrr Feb 06 '19 at 22:19

0 Answers0