Hi i'm working on my homework. It should take any integer and based on that it should take name, quality and price and at end should print the receipt.
import java.util.Scanner;
public class FormatReceipt{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int numItems = sc.nextInt();
String[] itemNames = new String[numItems];
int[] itemQuantity = new int[numItems];
double[] itemCosts = new double[numItems];
int numRead = 0;
while (numRead != numItems){
String itemName = sc.nextLine();
if (!itemName.isEmpty())
itemNames[numRead++] = itemName;
itemQuantity[numRead++] = sc.nextInt();
itemCosts[numRead++] = sc.nextDouble();
}
double grandTotal = 0.0;
for (int i = 0; i < numItems; i++){
double totalItemCost = itemQuantity[i] * itemCosts[i];
System.out.printf("%-15s %6.2f\n", itemNames[i], totalItemCost);
grandTotal += totalItemCost;
}
System.out.println(" ------");
System.out.printf(" %6.2f\n", grandTotal);
}
}
But it keeps throwing error. Any help would be appreciated. Thank you in advance!
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at FormatReceipt.main(FormatReceipt.java:6)