繁体   English   中英

在Java中,使用扫描仪从.txt文件中读取数据,以便我可以使用该数据作为构造函数参数来生成对象

[英]In Java, using a Scanner to read in data from a .txt file so I can generate objects with that data as constructor parameters

我有一个.txt文件,其内容具有以下格式:

Car
300
1

基本上,第一行是Expense对象的名称,第二行是Expense对象的成本,第三行是Expense对象的频率。 我的文本文件是该格式后三行的重复。 我可以使用扫描仪将所有这些行存储到列表中,但是由于某些原因,我的代码无法正确生成Expense对象。 在创建Expense对象时,我的构造函数将字符串ensemName,intenseCost和intexpenseFreq用作参数。 但是,在调试之后,我注意到我的输出很奇怪,因为即使我希望将其存储为整数,扫描程序仍似乎正在从.txt文件读取所有内容,就像是字符串一样。 这是代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

    public class readInFile {

        static ArrayList list = new ArrayList();
        static String expenseName;
        static int expenseCost;
        static int expenseFreq;

        public static void main(String[] args){
            File file = new File("C:/Practice/expenses.txt");
            ArrayList fileContents = new ArrayList();
            int count = 0;
            int makeExpenseObject = 0;

            try {
                Scanner input = new Scanner(file);

                while(input.hasNext()){
                    fileContents.add(input.nextLine());
                }

                for (Object o: fileContents){
                    if (makeExpenseObject == 3){
                        Expense e = new Expense(expenseName, expenseCost, expenseFreq);
                        makeExpenseObject = 0;
                        list.add(e);
                    }

                    if (o instanceof String){
                        expenseName = o.toString();
                        makeExpenseObject++;
                    }

                    else if (o instanceof Integer && count == 0){
                        expenseCost = (int)o;
                        count++;
                        makeExpenseObject++;
                    }
                    else if (o instanceof Integer && count != 0){
                        expenseFreq = (int)o;
                        count = 0;
                        makeExpenseObject++;
                    }
                }
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found.");
            }

            for (Object o: list){
                Expense e = (Expense) o;
                System.out.println(e.getName() + ", " + e.getCost() + ", " + e.getFreq());
            }

        }
    }

我当时正在考虑尝试使用BufferedReader而不是Scanner,但据我所知,这应该可以工作。 在尝试扫描文件并将行添加到列表时,我尝试保持计数器。 每当计数器为零时,我都将使用Scanner的nextLine方法。 每当计数器不为零时,我都会使用Scanner的nextInt方法,如果计数器等于2,则将其重置为零。这也不起作用-看来Scanner仍在看到该文件都是字符串。 为什么是这样?

此代码的目的是使一个简单的费用跟踪应用变得有趣。 我想将费用添加到.txt文件中,并让代码读取该文件。 以前,我在控制台应用程序中使用代码,但是每次我使用该应用程序时,输入我的花费都很麻烦。 现在,我想添加.txt阅读功能,并最终添加一个GUI。 感谢您的任何建议。

当您从文件中读取数据时,为什么不将其分配给正确的变量呢? 然后使用Integer.parseInt(String s)方法解析变量。

我仍然是一名学生,但是我认为您必须改变您看问题的方式...尝试将每一行分配给ur类的字符串成员,将第二行分配给int,将第三行分配给int。 为此,您可以使用从1到3的for循环和三个if语句

希望我能帮助你

您有问题的原因scanner.nextLine()是因为nextLine()返回一个字符串, 更多信息。

尝试这个:

public class readInFile {

    public static void main(String[] args){

       ArrayList list = new ArrayList();                

        File file = new File("C:/Practice/expenses.txt");
        ArrayList<String> fileContents = new ArrayList();

        try {
            Scanner input = new Scanner(file);

            while(input.hasNext()){
                fileContents.add(input.nextLine());
            }


          for (int i=0; i< fileContents.size()-3; i++){

      String expenseName = fileContents.get(0);
      int expenseCost = Integer.parseInt(fileContents.get(1)) ;
      int expenseFreq = Integer.parseInt(fileContents.get(2));

      Expense e = new Expense(expenseName, expenseCost,expenseFreq);                             
       list.add(e);

            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM