1

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

文本文件共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”是否为空。而且因为它最后一次读取文件时它仍然是一个字符串,所以循环继续。最后一次通过循环时,一切都是空的,所以试图转换整数是不可能的,所以我得到了错误。但我不知道如何让它在循环结束时读取文件而不转到下一行。这是文本文件的样子。

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

我无法在循环结束时读取它,因为如果它读取了,并且在我刚刚做的那辆车之后还有更多汽车,当循环重新启动时,它会进入下一行,一切都被抛弃了。

任何帮助表示赞赏,谢谢!

4

2 回答 2

1

在循环中使用带标签的 break语句来简单地在变为时for退出主do while循环。在循环中实例化其他对象的方式并没有为轻松重构留下太多空间,因此在这里使用标记的中断是有意义的。mylinenull

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);
于 2013-06-11T19:37:02.403 回答
0

也许您可以使用while循环而不是do-while循环,然后先从文件中读取下一行。像这样的东西:

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

  // All your logic...

}

readFile.close();

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

(myline = readFile.readLine()) != null
于 2013-06-11T19:37:05.557 回答