2011-12-07 23 views
0

我有一個循環,讀取程序啓動時文本文件中的行數,然後根據行數,它會將該許多對象存儲到新的(Vehicle [])數組(最多4個)中。試圖阻止從無限循環的循環,但也保持它從每次運行「休息」

public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException 
{ 
    Scanner reader = new Scanner(file); 
    String strLine = ""; 

     if(canAddVehicle() == true) 
     { 

     for(int i = 0; i < vehicles.length;i++) 
     { 
      System.out.println("This file is: " + file); 
      int counter = 0; 

      if(vehicles[i] == null) 
      { 
       try{ 
        // Open the file that is the first 
        // command line parameter 
        FileInputStream fstream = new FileInputStream(this.file); 

        // Get the object of DataInputStream 
        DataInputStream in = new DataInputStream(fstream); 
         BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

        //Read File Line By Line 
        while ((strLine = br.readLine()) != null) { 

         //Declare objects inside the array. 
         Honda[counter] = new Vehicle(); 
         Honda[counter].readRecord(reader); 
         vehicles[counter] = Honda[counter]; 
         counter++; 

        } 
        strLine = ""; 

        //Close the input stream and scanner 
        reader.close(); 
        in.close(); 
        }catch (Exception e){//Catch exception if any 
         System.err.println("Error: " + e.getMessage()); 
        } 
        } 
       break; 
      } 
     } 
      return true; 
     } 

我遇到的麻煩的部分是這一行:

if(vehicles[i] == null) 

程序啓動時,用戶可以選擇新的車輛添加到陣列後。如果你逐行閱讀代碼,你可以看到它開始於i = 0,並且讓我們說當程序第一次運行它時發現了2行值,所以它將2個對象存儲到數組中。 取值0和1。這意味着當用戶添加一輛新車時,它將跳過if(vehicles[i] == null),因爲spot [0]不爲空,它包含程序開始處的值。

然後,它會導致break;並將您踢出該方法,而不會通過for循環檢查數組中是否存在任何其他空值。 我可能在這裏做什麼?

+0

如果你想了解你的程序,我建議在調試器中逐句通過你的代碼。希望這會幫助你找到你需要放置'break'的地方;' –

+0

也是,你在哪裏增加'i'? –

+0

他增加我說的地方「i ++」 –

回答

0

感謝您的答案大家。我編程了大約12個小時,當我問這個問題時,我的腦海裏已經死了。不知道我在做什麼。我已經完成了我的代碼:

public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException 
{ 
    boolean found = false; 
    int position = 0; 
     if(canAddVehicle() == true) 
     { 
      for(int i = 0; i < vehicles.length && !found; i++) 
      { 
       if(vehicles[i] == null) 
       { 
        position = i; 
        found = true; 
       } 
      } 

       Scanner reader = new Scanner(file); 
       while(reader.hasNext()) 
       { 
        Honda[position] = new Vehicle(); 
        Honda[position].readRecord(reader); 
        vehicles[position] = Honda[position]; 
        position++; 

       } 
       reader.close(); 
       return true; 
     } 
     return false; 
} 
0

爲什麼你在那裏得到了休息 - 它只是讓它做到你所描述的那樣。刪除它,一切都會好起來的。

+0

因爲那麼如果它讀取文件它會寫入一個新的對象數組爲EVERY null值在數組中,而不是根據行數寫一個新的對象 – MJ93

1

兩件事, a。切換中斷繼續,並將中斷放在你想要的地方。

b。你應該關閉你的文件流,如果你完成使用它,因爲當你打開一個fStream它 「保持文件打開」,直到你關閉fStream無法使用

1

如果你格式化你的源,將更容易看到你的位置休息當前位於。然後嘗試考慮如何手動逐步執行程序。這通常幫助我。您可以決定是否要始終在您的循環中休息,或者只是在您剛剛裝入新車時。

Peter Lawrey給出了一個很好的使用調試器的評論,在確定你的程序要做什麼之後,如果它不像你期望的那樣工作,使用調試器(在大多數IDE中非常容易),你可以通過你的程序查看每個步驟所需的每個動作並檢查變量的值。

1

你的代碼真的沒什麼意義。從我瞭解你的問題的說明下面的代碼可能是也可能不是你想要做什麼:

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Scanner; 

public class VehicleList { 

    public class Vehicle { 
     private final String brand; 
     private final String make; 
     private final String year; 

     public Vehicle(String[] args) { 
      if (args.length < 3) { 
       throw new IllegalArgumentException("Too few args: " + args.length); 
      } 
      this.brand = args[0]; 
      this.make = args[1]; 
      this.year = args[2]; 
     } 

     @Override 
     public String toString() { 
      return String.format("%s %s %s", year, brand, make); 
     } 
    } 

    public List<Vehicle> readVehicles(String fileName) throws IOException { 
     List<Vehicle> vehicles = new ArrayList<Vehicle>(); 
     System.out.println(String.format("Reading vehicles from %s:", fileName)); 
     readVehicles(vehicles, new Scanner(new File(fileName)), false); 
     System.out.println(String.format("Reading vehicles from user:")); 
     readVehicles(vehicles, new Scanner(System.in), true); 
     return vehicles; 
    } 

    private void readVehicles(List<Vehicle> vehicles, Scanner scanner, boolean skipLineCheck) { 
     int count = 0; 
     while (skipLineCheck || scanner.hasNextLine()) { 
      String[] tokens = scanner.nextLine().split("\\s+"); 
      if (tokens.length < 3) { 
       break; 
      } 
      vehicles.add(new Vehicle(tokens)); 
      count++; 
     } 
     scanner.close(); 
     System.out.println(String.format("Read %s vehicles", count)); 
    } 

    public static void main(String[] args) throws IOException { 
     VehicleList instance = new VehicleList(); 
     List<Vehicle> vehicles = instance.readVehicles("vehicles.txt"); 
     System.out.println("Read the following vehicles:"); 
     System.out.println(Arrays.toString(vehicles.toArray())); 
    } 
} 

布爾skipLineCheck是需要讀取過去的最後一行在文件中,並投擲停止掃描儀NoSuchElementException異常。對於用戶輸入,我們不想做這個檢查,因爲它強制用戶給出一個額外的RETURN來結束輸入。

要運行這個,你需要在你的工作目錄下創建一個名爲「vehicles.txt」文件,例如下列內容:

Volvo Station 2008 
Audi A4 2009 
Honda Civic 2009 
Toyota Prius 2008 

測試運行提供了輸出象下面這樣:

Reading vehicles from vehicles.txt 
Read 4 vehicles 
Reading vehicles from user 
Nissan Micra 2002 
BMW cabriolet 1996 

Read 2 vehicles 
Read the following vehicles: 
[2008 Volvo Station, 2009 Audi A4, 2009 Honda Civic, 2008 Toyota Prius, 2002 Nissan Micra, 1996 BMW cabriolet] 
0

基於此聲明:然後導致休息;並且可以在不通過for循環檢查數組中是否存在其他空值的情況下將其踢出方法。

它聽起來像你想要繼續休息的地方。

突破將導致在for循環斷裂,而繼續將導致循環的代碼被讀取(從上到下),其中i增加1(在這種情況下)