I am doing a very basic Java program :
import java.util.Scanner;
public class App {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int nbrFloors = 0;
int nbrColumns = 0;
int nbrRows = 0;
System.out.println("So you have a parking with " + Integer.toString(nbrFloors) + " floors " + Integer.toString(nbrColumns) + " columns and " + Integer.toString(nbrRows) + " rows");
System.out.print("What's the name of your parking ? ");
String parkName = sc.next(); //or sc.nextLine() ?
System.out.print("How much should an hour of parking ? If you want your parking to be free please type in '0' : ");
float intPrice = Float.parseFloat(sc.next());
Parking parking = new Parking(nbrFloors, nbrColumns, nbrRows, parkName, intPrice);
}
}
As you can see on line 16 I use the Scanner.next()
method because using Scanner.nextLine()
skips user input. However I learned in this thread Java Scanner why is nextLine() in my code being skipped? that when you use the Scanner.next()
method it skips whatever is after the first word you entered.
So I wonder how you can ask for a user input, while both not skipping it (because Scanner.nextLine()
does that) and reading the whole input ?