I'm new to Java & I don't even know if what I'm trying to do is actually doable in cmd but I want to know if there's a way to ask the user for an input while displaying something at the right of the input, something like:
enter weight: _ kg
specifying the unit I want the weight in for example
here is the code sample I have so far
import java.util.*;
public class ScannerPrompt {
public static void main(String[] args) {
int[] listOfIntegers = new int[10];
Scanner s = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("Enter num: ");
int num = s.nextInt();
listOfIntegers[i] = num;
}
}
}
We can use \r
in a tricky way:
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
int i=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter num: __kg\r");
System.out.print("Enter num: ");
i = s.nextInt();
System.out.println("\n"+i);
}
}
How it works:
Enter num: __kg
and \r
to make cursor at the begining of the lineEnter num:
Dont use println
because it insert \n
at the end, and dont test in an IDE but use the console of your system.