java - 如何从可能存在或不存在的文件中读取字符串?

标签 java string file while-loop printwriter

我正在编写一个程序,该程序打开一个最初看起来像这样的文件:

3000.0
Lamps 15.3 400
Chairs 19.95 250
Desks 95.0 300

where the first line is the balance of a company, and the rest of the lines contain the product name, the price, and the quantity, respectively.

However, throughout the program, we make changes to the file and save what's been done at the end using the Printwriting object. One of the things we can do during the course of the program is add new items to the inventory. However, I must be able to read in a product that has multiple names, i.e. desk chairs, for instance. As my code is written now, the file only reads in the name then moves right to the price, so if I try to read in a multi-word name, it gives me a runtime error as I'm trying to read in a string with a double. Therefore, how do I read in a line that MAY OR MAY NOT have two strings in it? And, if there are two strings, how do I write it so that it stores both of them in the same String variable? Here is my code:

    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();

最佳答案

您可以做的一件事是将其存储在文件中时用连字符或其他字符替换空格。这样,您可以将其作为单个单词读出,然后用空格替换该字符,从而避免更改您提供的代码。

但是,如果您无法控制文件的写入方式,那么您可以尝试将 nextDouble() 函数放在 try block 中,以查看您是否正在查看价格或名称的下一个单词。请注意,如果产品名称的第二个单词是数字,则不会按设计工作。

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();

关于java - 如何从可能存在或不存在的文件中读取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474527/

相关文章:

sql - 使用给定模式在 Postgres 中生成列

c - 使用递归反转c中的字符串,而不使用任何其他数组或指针

c - 需要在 C 中的源文件之间共享文件指针的方法

c - 用户名和密码验证 Turbo C

java - 无法使用 Selenium 获取 WebElement 标签名称

java - YouTrack REST API : How to attach image to issue?

java - 为什么我不能将子项列表传递给接受 List< 的方法?扩展父>?

swift 4 : modifying numbers in String

c - 如何在不更改内容的情况下更新文本文件

java - Java 中的二进制到字符串转换?