Start New Discussion within our Software Development Community

I tryed to compress my file but got two errors:
Can someone see that could be wrong?

import java.util.Scanner;
public class Lab1
{

	public static void main(String[] args)
	{


	   //	create a Scanner object
		Scanner sc = new Scanner(System.in);


	   // read a string
		System.out.print("enter product code: ");
		String productCode = sc.next();

	  // read a double value
		system.out.print ("Enter price: ");
		double price = sc.nextDouble();

	  // read an int value
		system.out.print ("Enter quantity: ");
		int quantity = sc.nextInt();
			{

	  		//perform a calculation and display the result
			double total = price * quantity;
			System.out.println();
			System.out.println(quantity + "" + productCode
			+"@ " + price + "=" + total);
			System.out.println();
			}
	}
}

Thanks in advance

Every time you call a next method, try to call afterward the nextLine() command.
whenever you do this:
> next()
> nextDouble()

You read the next input but you don't change the line. So the next call will not read what you entered, but what is left until the end of the line which is an empty String.

Or try this ALL the time:

import java.util.Scanner;
public class Lab1
{

	public static void main(String[] args)
	{


	   //	create a Scanner object
		Scanner sc = new Scanner(System.in);


	   // read a string
		System.out.print("enter product code: ");
		String productCode = sc.nextLine();

	  // read a double value
		system.out.print ("Enter price: ");
		double price = Double.parseDouble(sc.nextLine());

	  // read an int value
		system.out.print ("Enter quantity: ");
		int quantity = Integer.parseInt(sc.nextLine());
			{

	  		//perform a calculation and display the result
			double total = price * quantity;
			System.out.println();
			System.out.println(quantity + "" + productCode
			+"@ " + price + "=" + total);
			System.out.println();
			}
	}
}
This article has been dead for over six months. Start a new discussion instead.