Question
Hello I am having an error that i dont know how to fix. JAVA The files are being upload to zybooks. Here is my code:
Hello I am having an error that i dont know how to fix. JAVA
The files are being upload to zybooks.
Here is my code:
ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
//creating scanner object for getting input from the keyboard Scanner in=new Scanner(System.in);
//array to store the object ItemToPurchase[] arr=new ItemToPurchase[2];
//variables to store the total, counter int i=0; int total1=0; int total=0;
//getting input from the user for(i=0;i
//getting item name System.out.println("Enter the item name:"); String name=in.nextLine();
//getting item price System.out.println("Enter the item price:"); int price=in.nextInt();
//getting item quantity System.out.println("Enter the item quantity:"); int quantity=in.nextInt();
//creating object arr[i]=new ItemToPurchase(name,price,quantity); in.nextLine(); System.out.println();
}
System.out.println("TOTAL COST");
//calculating the total for(i=0;i
System.out.println(" Total: $"+total);
} }
ItemToPurchase.java
class ItemToPurchase { //variable to store the name,price, quantity private String itemName; private int itemPrice; private int itemQuantity;
//default constructor public ItemToPurchase() { itemName="none"; itemPrice=0; itemQuantity=0; }
//parameterized constructor public ItemToPurchase(String itemname, int price,int quantity) { itemName=itemname; itemPrice=price; itemQuantity=quantity; }
//method to set the name public void setName(String name) { itemName=name; }
//method to set the price public void setPrice(int price) { itemPrice=price; }
//method to set the quantity public void setQuantity(int quantity) { itemQuantity=quantity; }
//method to get the name public String getName() { return itemName; }
//method to get the price public int getPrice() { return itemPrice; }
//method to get the quantity public int getQuantity() { return itemQuantity; } }
thank you
5: Compare output A Output differs. See highlights below. Special character legend Chocolate Chips Input Bottled Water 10 Item 1 Enter the item name: Enter the item price: Enter the item quantity: Item 2 Your output Enter the item name: Enter the item price: Enter the item quantity: Exception in thread "main" java.util.NosuchElementException: No line fou at java.util.Scanner.nextLine (Scanner.java:1540) ?at ShoppingCartPrinter.main (ShoppingCartprinter.java:52) Item 1 Enter the item name: Enter the item price: Enter the item quantity: Item 2 Enter the item name: Expected output Enter the item price: Enter the item quantity: TOTAL cosT chocolate Chips 1 @ $3 = $3 Bottled Water 10 s1 $10e Total: $13Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started