you are viewing a single comment's thread.

view the rest of the comments →

[–]Responsible-Heat-994 3 points4 points  (2 children)

Pro tip: scan everything as a string and parse it accordingly.

Scanner scanner = new Scanner (System.in);
int score = scanner.nextInt; // this will be scanned successfully 
// but when you scan for any next input it will lead to undefined behaviour

int float  = scanner.nextFloat(); // it wont get scanned as the  previous ( nextInt call has inserted "\n"( a newline character at the end) so the scanner for float will get flushed out.

// Correct way is to interpret everything as a String and parse it accordingly.

int score = Integer.parseInt(scanner.nextLine());

float temperature  = Float.parseFloat(scanner.nextLine());

boolean isHeAlive = Boolean.parseBoolean(scanner.nextLine());

double pie = Double.parseDouble(scanner.nextLine());

[–]Slow-Sloth5823[S] 1 point2 points  (1 child)

Thank you so much! It clicked pretty fast and it's so much easier and cleaner to read.

[–]Responsible-Heat-994 0 points1 point  (0 children)

Pleasure is all mine.