Home > Software design >  The insertion point is skipping a line
The insertion point is skipping a line

Time:07-13

This is my first week of coding and I am unsure of why when I go to ask "Do you enjoy life", it forces me to type the answer on the next line.

import java.util.Scanner;

public class messing {

    public static void main(String[] args) {

        Scanner keyboardInput = new Scanner(System.in);
        System.out.print("Enter Your Name: ");
        String name = keyboardInput.nextLine();

        System.out.print("How old are you: ");
        int age = keyboardInput.nextInt();
        System.out.println(name   " you are "   age   "?"   " Do you enjoy life: " );

        String enjoy = keyboardInput.next();
        System.out.print(enjoy   "?");
   }
}

CodePudding user response:

This is because the line of code that displays the question (line 13) uses the System.out.println() function, which, in addition to the text displayed, appends a newline character. This sends any further text onto the next line in the console. Replacing the function with the System.out.print() function removes this behavior.

CodePudding user response:

That's because you're using System.out.println when asking the user if they enjoy life.

Simply change the function to System.out.print like the rest so you can input your answer in the same line.

System.out.print(name   " you are "   age   "?"   " Do you enjoy life: " );
  •  Tags:  
  • java
  • Related