Java:问题阅读文本文件然后转换

时间:2013-06-11 19:13:13

标签: java readfile

我在获取读取文件的方法时遇到问题,然后将其转换为整数。以下是该计划的简要说明。它本质上是一个汽车经销商库存,通过将它们记录在文本文件中来跟踪批次中的车辆。当程序启动时,它需要读取文件并将所有当前的汽车放入一个数组中,以便显示它们。然后程序的其余部分将执行其他操作,例如移除汽车和添加新闻等。我所处的部分是程序首次启动时需要读取文件,但我似乎无法让它工作。< / p>

文本文件共包含6行;首先是4个数字,然后是2个单词。我希望该方法读取前四行并将它们转换为整数并将它们存储在临时数组中。然后,它将读取接下来的两行并将它们存储在临时数组中。然后我获取所有这些存储的值并将它们发送给构造函数。然后将构造函数存储在Arraylist中,并且可以随时访问Arraylist。在输出中它完成所有这一切就好了。但它希望第二次运行该方法,尽管存在阻止这种情况的障碍。

这是代码。它是一个类,而不是主程序。我将尽力在代码中尽力解释该程序。

public class Vehicle {

    //All the different private variables for the constructors and methods
    private int intholder[], year, type, kilometres, price, loop;
    private String make, model, myline, holder[]; 

    //The Arraylist that the different vehicle objects will be stored
    ArrayList<Vehicle> allCars = new ArrayList<Vehicle>();

    //The Default constructor
    public Vehicle(){
        make = "Vehicle Make";
        model = "Vehicle Model";
        type = 0;
        year = 0;
        kilometres = 0;
        price = 0;
    }

    //The constructor that has information sent to it
    public Vehicle(int _type, int _year, int _kilometres, int _price, String _make, String _model){
        make = _make;
        model = _model;
        type = _type;
        year = _year;
        kilometres = _kilometres;
        price = _price;
    }
    //Text file information
    /*
    * CAR TYPE CODE:
    * 1 - Sedan
    * 2 - Truck
    * 3 - Crossover
    * 4 - SUV
    * 5 - Sports
    * 
    * There is a total of 6 lines for each car and are as follows
    * 1 - int Type integer
    * 2 - int Year
    * 3 - int Kilometres
    * 4 - int Asking price
    * 5 - String Make
    * 6 - String Model
    */

    //The method in question. It reads through the file, converts the integers and stores them, 
    //stores the strings, and sends all the information to the constructor
    public void readCars()throws IOException{
        BufferedReader readFile = new BufferedReader(new FileReader("C:/Users/David/Desktop/FinalProject/Carlot.txt"));

        //Setting the length of the temporary arrays
        holder = new String[2];
        intholder = new int[4];

        //The main loop in the method. 
        do{

            //Read the first 4 lines of the file and convert them to integers. 
            //The try catch shouldn't have to be there because the first 4 lines 
            //of the file are all numbers, but I put it in there to see when it was messing up.
            for(int i = 0; i < 4; i++){
                myline = readFile.readLine();
                try{
                    intholder[i] = Integer.parseInt(myline);
                }
                catch(NumberFormatException e){
                    System.out.println(e);
                }

                //Had this in here to see how many lines down the file it would go before messing up.
                System.out.println(myline);
            }

            //Loop to store the Strings
            for(int i = 0; i < 2; i++){
                myline = readFile.readLine();
                holder[i] = myline;
                System.out.println(myline);
            }

            //Sends all the data to the constructor
            Vehicle V = new Vehicle(intholder[0], intholder[1], intholder[2], intholder[3], holder[0], holder[1]);

            //Several if statements to determine which subclass of vehicle it is.
            if(intholder[0]==1){
                Sedan S = new Sedan();
                allCars.add(S);
            }
            else if(intholder[0]==2){
                Truck T = new Truck();
                allCars.add(T);
            }
            else if(intholder[0]==3){
                Crossover C = new Crossover();
                allCars.add(C);
            }
            else if(intholder[0]==4){
                SUV U = new SUV();
                allCars.add(U);
            }
            else if(intholder[0]==5){
                Sports P = new Sports();
                allCars.add(P);
            }

        //Only break the loop if the myline equals null
        }while(myline != null);

        //if the loop breaks, close the file    
        readFile.close();
    }

现在我想我知道哪里出错了。在do / while结束时,它检查“myline”是否为空。并且因为它最后一次读取文件它仍然是一个字符串循环继续。它最后一次循环,一切都是null所以试图转换整数是不可能的,所以我得到错误。但我不知道如何让它在循环结束时读取文件而不进入下一行。这是文本文件的样子。

1
2007
150250
5000
Toyota
Corolla
2
2005
240400
4500
Chevorlet
Silverado

我不能在循环结束时读取它,因为如果它确实并且在我刚刚执行的那个之后还有更多的汽车,当循环重新启动时,它会进入下一行。

感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

for循环中使用 标记的中断 语句,只需在do while成为myline时退出主null循环outerloop: do { for (int i = 0; i < 4; i++) { if ((myline = readFile.readLine()) == null) break outerloop; // .. } for (int i = 0; i < 2; i++) { if ((myline = readFile.readLine()) == null) break outerloop; // .. } // .. } while (myline != null); 。在循环中实例化其他对象的方式并没有留下太多容易重构的空间,因此在这里使用带标签的断点是有道理的。

{{1}}

答案 1 :(得分:0)

也许您可以使用while循环而不是do - while循环,并在其他任何内容之前从文件中读取下一行。像这样:

String myline = null;
while( (myline = readFile.readLine()) != null ) {

  // All your logic...

}

readFile.close();

while循环的条件执行以下操作:首先,使用myline = readFile.readLine()读取文件的下一行。前一个语句返回myline的值,所以现在我们通过比较检查它是否为null:

(myline = readFile.readLine()) != null