Question
I need help i have my java code all written out but it wont print anything after the second price? it just keeps giving me
I need help i have my java code all written out but it wont print anything after the second price? it just keeps giving me an exception of main InputMismatchException?
////////. 1/2 files
import java.util.Scanner;
public class ShoppingCartPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
/* Type your code here. */ String name; int price = 0; int quantity = 0; ItemToPurchase item1 = new ItemToPurchase(); ItemToPurchase item2 = new ItemToPurchase(); System.out.println("Item 1"); System.out.println("Enter the item name:"); name = scnr.nextLine(); item1.setName(name); System.out.println("Enter the item price:"); price = scnr.nextInt(); item1.setPrice(price); System.out.println("Enter the item quantity:"); quantity = scnr.nextInt(); item1.setQuantity(quantity); //System.out.println(); System.out.println(" Item 2"); System.out.println("Enter the item name:"); name = scnr.nextLine(); item2.setName(name); System.out.println("Enter the item price:"); price = scnr.nextInt(); item2.setPrice(price); System.out.println("Enter the item quantity:"); quantity = scnr.nextInt(); item2.setQuantity(quantity); //System.out.println(); System.out.println(" TOTAL COST"); System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + (item1.getPrice() * item1.getQuantity())); System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + (item2.getPrice() * item2.getQuantity())); System.out.println(" Total: " + ((item1.getPrice() * item1.getQuantity()) + (item2.getPrice() * item2.getQuantity()))); }//end of main }//end of class ShoppingCartPrinter
////. 2/2 file
public class ItemToPurchase{ private String itemName; private int itemPrice; private int itemQuantity; public ItemToPurchase(){ }//end of ItemToPurchase public String getName(){ return itemName; }//end getName public void setName(String itemName){ this.itemName = itemName; }//end itemName public int getPrice(){ return itemPrice; }//end getPrice public void setPrice(int itemPrice){ this.itemPrice = itemPrice; }//end setPrice public int getQuantity(){ return itemQuantity; }//end getQuatity public void setQuantity(int itemQuantity){ this.itemQuantity = itemQuantity; }//end setQuantity }//end class ItemToPurchase
Step 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