I would like to know how to update First_name after scaning firstName and then update Last_name after scaning lastName. I don't know how to do it without rewriting the entire line.
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("First_name ");
System.out.println("Last_name");
System.out.println("Enter First name: ");
String firstName = scan.nextLine();
System.out.println("Enter Last name: ");
String lastName = scan.nextLine();
}
}
It is impossible to do what you want by dynamically changing the text. Writing the output with System.out.println()
will write it to the console and after that, it is out of your control. This will be the case for any language.
You could clear the console before writing everything all over again, which would make it look more dynamic. Doing that would look something like this:
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
Even though it won't solve your original question, I do want to use this post to point out the use of the carriage return however:
System.out.print("This is a test-string!");
System.out.println("\rThis string will overwrite that string!");
The carriage return, in Java used as \r
, will put the cursor back on the beginning of the line. This will overwrite the original string. The output of this code will be:
This string will overwrite that string!