0
我需要創建一個我可以打印到一個txt文件之前添加,刪除和修改的對象的清單。需要幫助!我不知道我是否以正確的方式設置了對象,或者它是否需要成爲數組。我有一本書,但它沒有提到我在這裏需要的對象的程序。試圖添加,修改和刪除對象,然後打印到文本文件
這是我的車對象
package PortfolioProject;
public class Vehicle {
// Variables
private String make;
private String model;
private String color;
private int year;
private int mileage;
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
}
這是使用對象
package PortfolioProject;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Inventory {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Vehicle automobile = new Vehicle();
System.out.print("Make?");
String inMake = scan.nextLine();
automobile.setMake(inMake);
System.out.print("Model?");
String inModel = scan.nextLine();
automobile.setModel(inModel);
System.out.print("Color?");
String inColor = scan.nextLine();
automobile.setColor(inColor);
System.out.print("Year?");
int inYear = scan.nextInt();
automobile.setYear(inYear);
System.out.print("Mileage?");
int inMileage = scan.nextInt();
automobile.setMileage(inMileage);
File file = new File("output.txt");
// write to file
try {
PrintWriter output = new PrintWriter(file);
output.println("Make: " + automobile.getMake());
output.println("Model: " + automobile.getModel());
output.println("Color: " + automobile.getColor());
output.println("Year: " + automobile.getYear());
output.println("Mileage: " + automobile.getMileage());
output.close();
} catch (IOException ex) {
System.out.printf("ERROR: %s\n", ex);
}
scan.close();
}
}
和有什麼問題? – niceman
你能分享你得到的錯誤信息嗎? – Blasanka
問題是我只輸出一個我無法修改或刪除的對象。我通過互聯網不知疲倦地搜索這些基於用戶輸入的方法,似乎無法找到解決方案。 –