2

How to write a text on the same line with the entered command before pressing the return-key? I have a switch statement where I choose one command, in this case to write some text after I enter in command "message". It should look like this:

  Command> message This is the message!

But instead of that, it looks like this:

  Command> message 
  This is the message!

My code:

switch (choice) {
//other commands 

  case "message":
            printMessage();
            break;
  default:
    System.out.println("Error, invalid input!");

}

public void printMessage() {
    String message = scan.nextLine();
}

Please be merciful, I'm a complete beginner :)

1 Answers1

0

In your code, use the print() function instead of the println() which prints it on a new line. Here, try this:

switch (choice) {
    //other commands 

  case "message":
            printMessage();
            break;
  default:
    System.out.print("Error, invalid input!");

}

public void printMessage() {
    String message = scan.nextLine();
}
Abiud Orina
  • 1,162
  • 8
  • 19
  • i think println() will print and then terminate with a newline character. The problem is not the newline after, it is the newline that the user created when pressing "enter" when he entered the input – Thijs Steel Feb 24 '18 at 21:13
  • @abiudrn , print() instead of println() was the first thing that I tried but it didn't work. As Thijs Steel says, the problem is the newline that the user creates when pressing "enter". – Agnès L'Ardent Feb 25 '18 at 16:07