Reputation: 31
I'm struggling with a lab in my school work. I keep getting an error: ShoppingCartPrinter.java:29: error: reached end of file while parsing } ^ 1 error
I have looked over my code and I don't see where I missed any curly brackets.
Here is the lab details.
Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need.
(1) Create two files to submit:
ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method Build the ItemToPurchase class with the following specifications:
Private fields
Default constructor
Public member methods (mutators & accessors)
(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string. (2 pts)
Ex: Item 1 Enter the item name: Chocolate Chips Enter the item price: 3 Enter the item quantity: 1
Item 2 Enter the item name: Bottled Water Enter the item price: 1 Enter the item quantity: 10 (3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10
Total: $13
Here is my code:
ShoppingCartPrinter
import java.until.Scanner;
public class ShoppingCartPrinter{
//main method
public static void main(String[] args){
//create object of scanner
Scanner scnr = new Scanner(System.in);
//variable declaration
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
//get item 1 details
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item1.setPrice(productPrice);
scnr.nextLine();
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
scnr.nextLine();
scnr.nextLine();
System.out.println("");
//get item 2 details
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item2.setPrice(productPrice);
scnr.nextLine();
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item2.setQuantity(productQuantity);
scnr.nextLine();
System.out.println("");
//add cost of item 1 and 2 and print total
cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
System.out.println("TOTAL COST");
//cart total is item 1 price * quantity + item 2 price * quantity
//item 1 info
int item1Total = item1.getPrice() * item1.getQuantity();
System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
//item 2 info
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
//output total
System.out.println("");
System.out.print("Total: $" + cartTotal);
}
}
ItemsToPurchase
public class ItemToPurchase {
//Private fields - itemName, itemPrice, and itemQuanity
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default Constructor
public ItemToPurchase(){
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//public member methods
public void setName(String itemName){
this.itemName = itemName;
}
public String getName(){
return itemName;
}
public void setPrice(int itemPrice){
this.itemPrice = itemPrice;
}
public int getPrice(){
return itemPrice;
}
public void setQuantity(int itemQuantity){
this.itemQuantity = itemQuantity;
}
public int getQuantity(){
return itemQuantity;
}
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
" = $" + (itemPrice * itemQuantity));
}
}
Upvotes: 2
Views: 1390
Reputation: 6816
Problem 1: You problem is that the next int doesn't consider the new line character which goes in the input for your next read. Hence name is returned as blank.
Problem 2: You are assigning String to a int. refer: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-
The following code works fine for me: ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter{
//main method
public static void main(String[] args){
//create object of scanner
Scanner scnr = new Scanner(System.in);
//variable declaration
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
//get item 1 details
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item1.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
scnr.nextLine();
System.out.println("");
//get item 2 details
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item2.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item2.setQuantity(productQuantity);
System.out.println("");
//add cost of item 1 and 2 and print total
cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
System.out.println("TOTAL COST");
//cart total is item 1 price * quantity + item 2 price * quantity
//item 1 info
int item1Total = item1.getPrice() * item1.getQuantity();
System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
//item 2 info
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
//output total
System.out.println("");
System.out.print("Total: $" + cartTotal);
}
}
ItemsToPruchase.java
public class ItemToPurchase {
//Private fields - itemName, itemPrice, and itemQuanity
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default Constructor
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//public member methods
public void setName(String itemName) {
this.itemName = itemName;
}
public String getName() {
return itemName;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getPrice() {
return itemPrice;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
public int getQuantity() {
return itemQuantity;
}
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
" = $" + (itemPrice * itemQuantity));
}
}
Output :
Item 1
Enter the item name:
item 1
Enter the item price:
20
Enter the item quantity:
5
Item 2
Enter the item name:
item 2
Enter the item price:
2
Enter the item quantity:
10
TOTAL COST
item 1 5 @ $20 = $100
item 2 10 @ $2 = $20
Total: $120
You had misplaced brackets. Once i fixed it this works. Please make sure you use an IDE and not zybooks
import java.util.Scanner;
public class ShoppingCartPrinter {
//main method
public static void main(String[] args) {
//create object of scanner
Scanner scnr = new Scanner(System.in);
//variable declaration
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
//get item 1 details
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item1.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
scnr.nextLine();
System.out.println("");
//get item 2 details
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
item2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item2.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item2.setQuantity(productQuantity);
System.out.println("");
//add cost of item 1 and 2 and print total
cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
System.out.println("TOTAL COST");
//cart total is item 1 price * quantity + item 2 price * quantity
//item 1 info
int item1Total = item1.getPrice() * item1.getQuantity();
System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
//item 2 info
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
//output total
System.out.println("");
System.out.print("Total: $" + cartTotal);
}
public static class ItemToPurchase {
//Private fields - itemName, itemPrice, and itemQuanity
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default Constructor
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//public member methods
public void setName(String itemName) {
this.itemName = itemName;
}
public String getName() {
return itemName;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getPrice() {
return itemPrice;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
public int getQuantity() {
return itemQuantity;
}
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
" = $" + (itemPrice * itemQuantity));
}
}
}
Upvotes: 2
Reputation: 302
You should use scnr.nextLine() after scnr.nextint(). When you enter a value and then press enter, a newline character is added after your value.
When you use nextInt(), it reads only the integer input and leaves behind the "newline" (\n) character in the input buffer. So, if you use nextLine() immediately after nextInt(), it will consume the leftover newline character, essentially reading an empty line.
You already had nextLine() after you inputted the name. Now you need it after each nextInt().
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
scnr.nextLine(); // Consuming leftover newline character
Upvotes: 1