Java购物车程序解析错误求助:文件末尾解析失败
解析错误:
reached end of file while parsing 原因排查 报错信息:
ShoppingCartPrinter.java:29: error: reached end of file while parsing
编写在线购物车程序时遇到上述解析错误,检查代码未发现大括号缺失,相关实验要求及代码如下:
实验要求
- 创建两个文件:
ItemToPurchase.java(类定义)和ShoppingCartPrinter.java(包含main()方法)。ItemToPurchase类需包含私有字段itemName、itemPrice、itemQuantity,默认构造函数初始化值,以及对应的getter和setter方法。 - 在
main()方法中,提示用户输入两个商品信息,创建两个ItemToPurchase对象,计算并输出总费用。
编写的代码
ShoppingCartPrinter.java
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); } }
ItemToPurchase.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)); } }
错误原因及修正方法
核心错误
你在ShoppingCartPrinter.java的导入语句中存在拼写错误:
import java.until.Scanner;
正确的导入语句应为:
import java.util.Scanner;
这个拼写错误会导致编译器无法识别合法的包路径,进而干扰整个文件的语法解析流程,最终抛出看似“文件未结束”的错误——实际上代码的大括号是完整闭合的,问题根源在于import的语法错误。
额外优化建议
代码中获取第一个商品信息后存在一行冗余的scnr.nextLine();(位于第29行附近),这会导致用户在输入完第一个商品的数量后,需要额外输入一次回车才能继续第二个商品的信息输入,删除该行即可优化交互流程。
内容的提问来源于stack exchange,提问作者Joshua Pelkey
相关产品推荐
相关产品推荐

