2013-06-11 42 views
1

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

该文本文件总共由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 

我不能拥有它在循环结束阅读,因为如果这样做,并还有更多车一个我只是做了之后,它会进入下一行,当循环重新开始一切都抛关闭。

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

+1

你的问题会更简洁,更容易回答,如果你只是有一个最小的,编译能类告诉我们你的预期与实际产出。 –

+0

我现在所期望的所有输出都是文本文件中的内容。现在,我得到了文本文件中的内容以及另外6行的null。 – Dave555

+0

你确定文件底部没有空行吗?您是否考虑过为此添加检查以及为空?或者应该允许空行? – efan

回答

1

使用标签break语句进行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); 
+0

我试过了,它工作!谢谢!虽然我有一个关于外圈的问题。当你使用它时,它只适用于它下面的循环吗?再次感谢。 – Dave555

+1

是的,就像给一个标签(标签可以是任何文本,如主循环等)到它下面的循环。但是,我建议您将与单个车辆有关的所有数据保存在文件中的同一行(以逗号分隔的值)。然后你可以通过检查while(reader.readLine()!= null)'和简单的'line.split(“,”)'每行来处理这些值来检查EOF(文件结束)。 –

+0

好吧很高兴知道,感谢您的帮助! – Dave555

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