2017-06-03 36 views
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(); 

    } 
} 
+0

和有什麼問題? – niceman

+0

你能分享你得到的錯誤信息嗎? – Blasanka

+0

問題是我只輸出一個我無法修改或刪除的對象。我通過互聯網不知疲倦地搜索這些基於用戶輸入的方法,似乎無法找到解決方案。 –

回答

0

在文件中寫入之前,你可以檢查是否瓦萊斯是從目標(automobile)來不來由程序使用system.out

System.out.println(automobile.getMake()); 

如果沒有任何值顯示,您g要知道問題與類或對象有關。否則,您會發現PrintWriter存在問題。

我檢查你的程序後,我注意到,在output.txt不寫值的原因:不寫任何值

原因:

File file = new File("output.txt");只有持有文本文件的路徑。如果文件不存在,PrintWriter未創建文件。 read about PrintWriter

解決方案:

首先添加的目錄至極文本文件存在,象下面這樣:

File file = new File("folder/output.txt"); 

然後你就可以確保目錄或者存在或不:

file.getParentFile().mkdirs(); 

All together:

File file = new File("folder/output.txt"); 
file.getParentFile().mkdirs(); 
// 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); 

} 
相關問題