2012-11-26 30 views
-3

有人可以幫助我,即時通訊嘗試使用在聲明之外的if語句中稱爲prop的數組列表,並在其中添加了一個對象。如何訪問在'if'語句中創建的ArrayList?

這裏是if語句。代碼要求用戶輸入信息,然後聲明一個Property對象和一個屬性ArrayList,它將我創建的屬性對象添加到ArrayList中。

if(option.equals("one")){ 

    int x = 1; 
    do{ 
     try{ 
      System.out.println("Enter owner name"); 
      String ownerName = scan.nextLine(); 
      System.out.println("Enter address"); 
      String address = scan.nextLine(); 
      System.out.println("Are you the principle private residant?"); 
      String principlePrivateResidance = scan.nextLine(); 
      System.out.println("Enter property location(city, large town, small town, vilage, countryside) "); 
      String location = scan.nextLine(); 
      System.out.println("would you like to pay the propery tax now?(yes//no)"); 
      String paid = scan.nextLine(); 
      System.out.println("Enter your filename"); 
      String fn = scan.nextLine(); 
      System.out.println("Enter the estimated property value"); 

      int propertyEstimatedVal = scan.nextInt(); 
      // i make the object here 
      Property property = new Property(ownerName, address, paid, principlePrivateResidance,location, propertyEstimatedVal); 

      // Then make the arraylist and add the object to it take this arraylist and put it.... 

      ArrayList<Property> prop = new ArrayList<Property>; 
      prop.add(property); 
      CalculateTax tax = new CalculateTax(property.getLocation(),property.getPrinciplePrivateResidance(),property.getPropertyEstimatedVal(), property.getPaid()); 

      System.out.println("owner:" + property.getOwnerName()); 
      System.out.println("address:" +property.getAddress()); 
      System.out.println("propertyEstimatedVal:" +property.getPropertyEstimatedVal()); 
      System.out.println("principlePrivateResidance:" +property.getPrinciplePrivateResidance()); 
      System.out.println("location:" +property.getLocation()); 
      System.out.println("paid:" +property.getPaid()); 
      System.out.println("Your property tax is" + CalculateTax.getSumoftax()); 
      System.out.println("your details have been taken"); 

      FileWriter fstream = new FileWriter(fn,true); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write(property.getOwnerName() + " | " + property.getAddress() + " | " + property.getPropertyEstimatedVal() + " | " + property.getPrinciplePrivateResidance() + " | " + property.getLocation() + " | " + property.getPaid() + " | " + CalculateTax.getSumoftax()); 
      out.newLine(); 
      out.close(); 
      x= 2; 
     } 
     catch(Exception e){ 
      System.out.println("error has occured"); 
     } 
    } 
    while(x == 1); 

} 
else if(option.equals("two")){ 
    // this just opens a file 
    System.out.println("Please enter you file name"); 
    String filename = scan.nextLine(); 

    try{ 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream(filename); 
     // Get the object of DataInputStream 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 

     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      // Print the content on the console 
      System.out.println (strLine); 
     } 
     //Close the input stream 
     in.close(); 
    } 
    catch (Exception e){//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 
else if(option.equals("three")){ 
    //i need to get that arraylist with the objects i added to it here but 
    //i just dont know how 
    System.out.print("Service is currently available "); 

} 
else{ 
    System.exit(0); 
} 
+0

我不明白你的問題。請編輯它以澄清您的確切問題。 –

+1

您需要在足夠高的範圍內聲明您的變量,以便在您所需的類的所有部分都可見。這通常意味着將其聲明爲一個類變量,但也可能只是需要在方法的頂部聲明該變量。因爲它是你的代碼很難閱讀,所以我不能說明你需要放置變量的位置。請格式化並重新發布。 – Perception

+0

其中一個分支創建ArrayList,另一個嘗試訪問它 - 這怎麼可能?代碼看起來像* *創建列表*或*使用它。 –

回答

0

如果您需要訪問if聲明ArrayList外,你需要移動的變量聲明if語句之前發生。例如,刪除此行...

Property property = new Property(ownerName, address, paid, principlePrivateResidance,location, propertyEstimatedVal); 

// Then make the arraylist and add the object to it take this arraylist and put it.... 

ArrayList<Property> prop = new ArrayList<Property>; // REMOVE THIS LINE 
prop.add(property); 

在這裏添加它,而不是...

ArrayList<Property> prop = new ArrayList<Property>; // ADD THIS LINE 

if(option.equals("one")){ 

    int x = 1; 
    do{ 

現在ArrayList是您if語句之前定義的,所以你可以在任何地方使用裏面它和在if聲明之外。

你可能會想要移動ArrayList以外的do-while循環無論如何,否則新ArrayList會在您每次經過do-while循環時創建的。我確定你真正想要的是創建一個ArrayList並只需添加一些項目 - 因此您需要將ArrayList的聲明移到do-while循環之外,這也是通過上面的代碼更改實現的。

您需要做的下一件事是檢測一個項目是否被添加到您的ArrayList或不。如果您不執行if(option.equals("one")){語句中的代碼,則將不會添加任何內容,因此它將爲空。因此,如果你想用你的ArrayList後,應先檢查是否有任何項目,然後如果它們存在使用它們 - 也許這樣的事情...

if (prop.size() > 0){ 
    // there are items 
    doSomething(); 
} 
else { 
    // no items 
    doSomethingElse(); 
} 
2

您需要將您的

 List<Property> prop = new ArrayList<Property>; 

     if(option.equals("one")){ 
      //your if block code 
      do{ 
       //rest of your code except list declaration/initialization 
       }while(x==1); 
       .... 
     }else if(option.equals("two")){ 
      ..... 
     } else if(option.equals("three")){ 
      //your list `prop` is available here 
      .... 
     } 

這是必需的,原因有二:

  1. 當前正在重新實例化你的if聲明如下之外數組列表decalration/insntantiation在您的do-while循環的每次迭代中刪除以前添加的數據。
  2. 只有當您需要在if以外的區域訪問它時,即在其他區塊中,您的列表纔在if區塊的範圍內。

通過將列表初始化在if塊之外移動,您將解決這兩個問題。