Java

Scanner nextInt + nextLine pitfall

Në shqip: Kurthi i Scanner-it

ShpjegimiSQ

Pas scanner.nextInt(), Enter-i mbetet në hyrje, dhe nextLine() i radhës e lexon menjëherë si rresht bosh. Zgjidhje: shto një nextLine() bosh pas nextInt(), ose lexo gjithçka me nextLine() dhe konverto.

EnglishEN

After scanner.nextInt(), the Enter stays in the input, and the next nextLine() reads it immediately as an empty line. Fix: add an empty nextLine() after nextInt(), or read everything with nextLine() and convert.

Si ta mendosh

Si kur dikush lë një gotë bosh në tavolinë dhe kamerieri mendon se është porosia jote.

Lexoje në anglisht

Like someone leaving an empty glass and the waiter thinking it's your order.

Shembull kodijava

int mosha = sc.nextInt();
sc.nextLine();               // hiq Enter-in
String emri = sc.nextLine();

Terma të lidhur