【发布时间】:2014-12-13 19:42:26
【问题描述】:
抱歉,如果代码看起来很乱。
这里是新手。 我试图让用户根据他们的输入删除 App 类型的对象,而 ArrayList 只接受“App”类型的对象。但是,我无法删除它,因为我在 if 语句中创建了一个 App 类型的对象并且我不知道如何访问它。有什么方法可以删除和编辑由不同 if 语句和 else if 创建的对象?
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner myScan = new Scanner(System.in);
System.out.println("Enter your option from the list below");
System.out.println("1.App Store");
System.out.println("2.Customer stuff");
int input = myScan.nextInt();
if (input == 1){
wholeSystem();
}
}
public static void wholeSystem()
{
boolean choice = true;
int input2 = 0;
Shop appStore = new Shop(new ArrayList<App>());
while (choice == true)
{
Scanner myScan2 = new Scanner(System.in);
System.out.println("Enter another option from the list below");
System.out.println("1. Add new App details");
System.out.println("2. Delete App details");
System.out.println("3. Edit App details");
System.out.println("4. List App details");
System.out.println("5. Search App details");
System.out.println("6. Quit");
input2 = myScan2.nextInt();
if (input2 == 6)
{
System.out.println("You have exited.");
break;
}
else if (input2 == 1)
{
System.out.println("You have selected feature "+input2+".");
Scanner myScan3 = new Scanner(System.in);
App myObj = new App();
System.out.println("Please enter the app name:");
myObj.setAppName(myScan3.nextLine());
System.out.println("Please enter the developer name:");
myObj.setDeveloperName(myScan3.nextLine());
System.out.println("Please enter the app's function:");
myObj.setAppFunction(myScan3.nextLine());
System.out.println("Please enter the app's type");
myObj.setType(myScan3.nextLine());
System.out.println("Please enter the app's cost:");
myObj.setCost(myScan3.nextInt());
System.out.println("Please enter the app's popularity:");
myObj.setPopularity(myScan3.nextInt());
System.out.println(myObj.getAppName()+myObj.getDeveloperName());
appStore.addApp(myObj);
File AppStore = new File("AppStore");
try{
BufferedWriter out;
out = new BufferedWriter ( new FileWriter(AppStore,true));
out.write(myObj.getAppName());
out.newLine();
out.write(myObj.getDeveloperName());
out.newLine();
out.write(myObj.getAppFunction());
out.newLine();
out.write(myObj.getType());
out.newLine();
out.write (String.valueOf(myObj.getCost()) );
out.newLine();
out.write(String.valueOf(myObj.getPopularity()) );
out.newLine();
out.write("========================================================");
out.newLine();
//Close the output stream
out.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
else if (input2 == 2)
{
int choiceInput = 0;
System.out.println("You have selected feature "+input2+". Which app do you want deleted?");
// here's where I tried to remove an object
appStore.printApps();
choiceInput = myScan2.nextInt();
appStore.deleteApp(choiceInput); with user input
这是我创建 ArrayList 的商店类。 公共类商店{
private ArrayList<App> apps = new ArrayList<App>();
public Shop(ArrayList apps)
{
this.apps = apps;
}
public ArrayList<App> getApps(){return apps;}
public void setApps(ArrayList<App>apps){this.apps = apps;}
public void addApp(App app){apps.add(app);}
public void deleteApp(App app){apps.remove(app);}
【问题讨论】:
-
...什么不起作用?请提供错误消息、堆栈跟踪或行为描述。请注意,您使用整数参数调用 deleteApp,但您的声明应该传递一个 App。
-
表示int与app不兼容,无法转换。是的,我试图用一个整数来调用它,因为我希望用户能够在扫描仪中输入一个数字,并根据给定的数字从数组列表中删除一个对象。
-
在下面查看我的答案,不需要更多代码就可以根据 ArrayList 中的索引删除应用程序,因为 Arraylist 支持。