problem with Scanner nextLine() method
I'm working on a code that involves a scanner, and it uses nextLine() method for reading input, but when that lines executes, it jumps to the next line, not giving opportunity to enter the String....... here's the code
Code :
System.out.println("Description:");
String description = myscan.nextLine();
System.out.println("Price:");
double price = myscan.nextDouble();
System.out.println("Quantity:");
int qty = myscan.nextInt();
any help appreciated
Re: problem with nextLine() method...
Hello mia_tech,
Take a look at this code. It solves your problem:
Code :
import java.util.Scanner;
public class Mia_tech {
public static void main(String[] args) {
System.out.println("Description:");
Scanner myscan = new Scanner(System.in);
String description = myscan.nextLine();
System.out.println("Price:");
double price = myscan.nextDouble();
System.out.println("Quantity:");
int qty = myscan.nextInt();
System.out.println("Description: " + description);
System.out.println("Price: " + price);
System.out.println("Quantity: " + qty);
}
}