2014-10-20 66 views
2

我正在寫打開,看起來像這樣最初的文件的程序:如何從可能存在或不存在的文件中讀取字符串?

 
3000.0 
Lamps 15.3 400 
Chairs 19.95 250 
Desks 95.0 300 

,其中第一行是一個公司的平衡,線的其餘部分包含產品名稱,價格,和數量。

但是,在整個程序中,我們對文件進行了更改,並使用Printwriting對象保存最後所做的操作。在計劃過程中我們可以做的其中一件事是向庫存添加新物品。但是,我必須能夠閱讀具有多個名稱的產品,例如桌椅。由於我的代碼是現在編寫的,文件只讀取名稱然後右移到價格,所以如果我嘗試讀取多字名稱,它會給我一個運行時錯誤,因爲我試圖讀取一個字符串與雙。因此,我應該如何閱讀可能不含有兩個字符串的行?而且,如果有兩個字符串,我該如何編寫它,以便將它們都存儲在相同的字符串變量中?這是我的代碼:

File file = new File("inventory.txt"); 
    Scanner inputFile = new Scanner(file); 

    ArrayList<String> productName = new ArrayList<String>(); 
    ArrayList<Double> productQuantity = new ArrayList<Double>(); 
    ArrayList<Double> productPrice = new ArrayList<Double>(); 

    double balance = inputFile.nextDouble(); 

    while (inputFile.hasNext()) 
    { 
     String product = inputFile.next(); 
     double price = inputFile.nextDouble(); 
     double quantity = inputFile.nextDouble(); 

     //Takes those variables and stores each of them in ArrayList objects 
     productName.add(product); 
     productPrice.add(price); 
     productQuantity.add(quantity); 
    } 

    inputFile.close(); 
+0

你檢查嗎? [用java檢查現有文件](https://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows) – 2014-10-20 20:52:14

+0

@Freezzo - 它是字符串可能或不可能在那裏,而不是文件。 – 2014-10-20 20:53:12

+0

我不需要檢查文件是否存在。我知道該怎麼做。如果產品名稱不止一個單詞,我需要能夠讀入多個字符串。例如,如果有一行表示「Desk Chairs 20.5 300」,我需要能夠將名稱「Desk Chairs」存儲在一個String變量中,但對於該行來說,僅僅因爲有另外3行只有一個單詞產品名稱。 – user3525572 2014-10-20 20:57:58

回答

1

你可以做的一件事是用連字符或其他字符替換空格,同時將它存儲在文件中。這樣,您可以將其作爲一個單詞讀出來,然後用空格替換該字符,從而避免必須更改您提供的代碼。

但是,如果您無法控制文件的寫入方式,那麼您可以嘗試在try塊中放置nextDouble()函數,以查看是否查看價格或名稱的下一個單詞。請注意,如果產品名稱的第二個單詞是數字,則不會按設計工作。

File file = new File("inventory.txt"); 
Scanner inputFile = new Scanner(file); 

ArrayList<String> productName = new ArrayList<String>(); 
ArrayList<Double> productQuantity = new ArrayList<Double>(); 
ArrayList<Double> productPrice = new ArrayList<Double>(); 

double balance = inputFile.nextDouble(); 

while (inputFile.hasNext()) 
{ 
    String product = inputFile.next(); 
    try{ 
     double price = inputFile.nextDouble(); 
    } 
    catch(Exception e){ 
     product = product+" "+inputFile.next(); 
     double price = inputFile.nextDouble(); 
    } 
    double quantity = inputFile.nextDouble(); 

    //Takes those variables and stores each of them in ArrayList objects 
    productName.add(product); 
    productPrice.add(price); 
    productQuantity.add(quantity); 
} 

inputFile.close(); 
+0

非常感謝您的幫助,但這並不是我熟悉的內容,因爲我們還沒有學到它,因此不習慣將它用於此課程。 – user3525572 2014-10-20 21:12:33

+0

而且,我不允許更改文件的格式。 – user3525572 2014-10-20 21:13:37

+0

[異常處理](http://www.tutorialspoint.com/java/java_exceptions.htm)是編程中一個非常基本的概念。我知道你還沒有學到它,但是如果它解決了你的問題,你一定要考慮使用它,更重要的是,要更多地瞭解它,因爲它們非常有用。 – EnKrypt 2014-10-20 21:17:05

0

好了,所以如果你想保持自己的格式,你將需要調整你的代碼,它表示,在一個簡單的方法的數據。首先,你需要一個Object。我們稱之爲Product

public class Product { 
    private String name; 
    private int quantity; 
    private double price; 

    public Product(String name, int quantity, double price) { 
     this.name = name; 
     this.quantity = quantity; 
     this.price = price; 
    } 
} 

現在你已經有了一個很好的代表每個塊,你需要一個很好的方式來寫他們。現在你有效地寫StringFile,所以讓我們使用toString()方法制定出它應該是什麼樣子:

public String toString() { 
    return name + " " + quantity + " " + price; 
} 

然後,你打開在一個FileWriter覆蓋模式,循環使用您的新的ListProduct類型,並簡單地寫入toString()值。你會注意到我沒有在這裏給出一個特定的代碼示例。你可以解決它!這種方法

  • 大一個

    優點是,你有你的整個文件的對象表示。這意味着在應用程序的生命週期中,您可以更新對象列表,而不需要觸摸文件。

缺點

  • 你需要每次保存重寫整個文件。假如你堅持你的數據的時候很聰明,你會沒事的。
0

假設你不想改變輸入文件格式,你可以檢查你正在閱讀的字符串是否是數字。

public static boolean isNumeric(String str) 
{ 
try 
{ 
    double d = Double.parseDouble(str); 
} catch(NumberFormatException nfe) 
    { 
     return false; 
    } 
return true; 
} 

爲了存儲在相同的變量兩個串,用StringBuffer在情況下,第二字符串不是數字和後來轉換StringBuffer來字符串。

0

不要使用掃描儀,使用一個緩衝的讀者,讀一行一行,然後分裂結果,並根據計解析:

File file = new File("inventory.txt"); 
BufferedReader reader = new BufferedReader(new FileReader(file)); 
double balance = Double.parseDouble(reader.readLine()); 
while (true) { 
    String line = reader.readLine(); 
    if (line == null) { 
    break; 
    } 
    String[] parts = line.split(" "); 
    int count = parts.length; 
    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < count - 2; i++) { 
    if (i > 0) { 
     i.append(' '); 
    } 
    sb.append(parts[i]); 
    } 
    String productName = sb.toString(); 
    double price = Double.parseDouble(parts[count - 2]); 
    double quantity = Double.parseDouble(parts[count - 1]); 

    productName.add(product); 
    productPrice.add(price); 
    productQuantity.add(quantity); 
} 
reader.close();