我试图从菜单中阅读我的用户,然后进行选择1(添加产品名称,商店名称,购买日期和成本(,选项2显示ArrayList,购买对象已被输入,并且选项3退出了程序,并感谢用户使用我的程序,同时有例外,以确保用户不输入和有效的条目。现在,我需要能够存储到txt文件,并有能力将其拉回以进行审查或修改。
我构建了菜单,并且我相信我已经正确设置了我的arraylist以适合适当的用户输入,每个字符串的方法都将输入和例外,以确保输入有效
public static void main(String[] args) throws Exception {
//Create menu options
TheMenu();
}
public static void TheMenu() {
Scanner input = new Scanner(System.in);
String purchase[] = new String[4];
int option;
do { // loop until Exit (option 3) is selected
System.out.println("nMenu Options");
System.out.println("n1 Add a purchase");
System.out.println("n2 Display a purchase");
System.out.println("n3 Exit");
option = input.nextInt();
if (option == 1) {
displayPurchaseObjects(purchase);
}
if (option == 2) {
System.out.println();
}
while (option == 3)
System.out.println("Thank you for using purchase programmer!");
System.exit(3);
//Create a list to store Purchase objects
ArrayList<String> displayPurchaseObjects = new ArrayList<>();
while(input.hasNext()) {
System.out.println("Enter the product name ");
String productName = input.next();
System.out.print("Enter the store name");
String storeName = input.next();
System.out.println("Enter the purchase date (i.e. 06/30/2019) ");
int purchaseDate = input.nextInt();
System.out.println("Enter the cost ");
double cost = input.nextDouble();
}
}
}
public void productName(String productName) {
try {
for(int i = 1; i < productName.length(); i++) {
}
}
catch(Exception e) {
System.out.println("Please enter a product name ");
}
}
public void storeName(String storeName) {
try {
for(int i = 1; i < storeName.length(); i++) {
}
}
catch(Exception e) {
System.out.println("Please enter a store name");
}
}
public void purchaseDate(String date) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(date);
try {
format.parse(date);
}
catch(Exception e) {
System.out.println("Invalid date");
}
}
public void cost(double newCost)
throws InputMismatchException{
double cost;
if(newCost >= 1)
cost = newCost;
else
throw new InputMismatchException("Cost must be a integer and more the $0 ");
}
我正在收到汇编错误,因为"}"我找不到原因,并且该程序应该允许我进行选择(1,2或3(,并且应该能够将我的购买对象添加到我的" displayPurchaseObjects" arrayList
如果我清楚地理解了您的问题,那么手头的问题是编译错误 - 找不到"}"。
在您的代码中,您错过了此后的"}" -
do { // loop until Exit (option 3) is selected
System.out.println("nMenu Options");
System.out.println("n1 Add a purchase");
System.out.println("n2 Display a purchase");
System.out.println("n3 Exit");
option = input.nextInt();
if (option == 1) {
displayPurchaseObjects(purchase);
}
if (option == 2) {
System.out.println();
}
} while (option == 3);
System.out.println("Thank you for using purchase programmer!");
// Non zero exit code is only for abnormal termination. Use 0.
System.exit(0);