2012-11-13 154 views
1

我正在開發NetBeans,並且想知道爲什麼當我按F6時運行此代碼?我沒有主要的方法。當我按F6時,代碼將運行LoadListings方法並從位於硬盤驅動器上的「houses.txt」加載陣列列表。它之前不運行LoadArray方法,因此只有文本文件數據在最後打印。這是爲什麼運行?

package housetracker; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.StringTokenizer; 


public class HouseList { 

List<House> houseList; 
private String defaultFileName = "houses.txt"; 
private String fileSeparator = System.getProperty("file.separator"); 
private String workingDirectory = (System.getProperty("user.dir") + this.fileSeparator); 
private String listingsFile = workingDirectory + "txt_files" + this.fileSeparator + defaultFileName; 
private BufferedReader br; 
private FileReader fr; 
private String line; 
private Integer address; 
private String street; 
private double price; 
private int rooms; 
DecimalFormat moneyFormat = new DecimalFormat("0.00"); 

public HouseList() { 
    houseList = new ArrayList(); 

} 

public void FillArray() { 
    fillArray(); 
} 

private void fillArray() { 
    houseList.add(new House(123, "Main", 75000.00, 2)); 
    houseList.add(new House(621, "Mystreet", 175000.00, 5)); 
    houseList.add(new House(4568, "1st", 725000.00, 8)); 
    houseList.add(new House(5546, "Broadway", 85600.00, 3)); 
    houseList.add(new House(8744, "Texas", 195610.00, 6)); 
    houseList.add(new House(45454, "Maine", 125000.00, 4)); 
    houseList.add(new House(4465, "Main", 375000.00, 2)); 
} 

public void LoadListings(String fileName) { 
    loadListings("C:\\\\temp\\houses.txt"); 
} 
//Load the listings file into the array. 

private void loadListings(String fileName) { 
    // Clear variables 
    this.br = null; 
    this.fr = null; 
    this.line = null; 

    try { 
     // Set FileReader to access the user specified file name. 
     this.fr = new FileReader(fileName); 
     System.out.println(fr); 
     // Use a BufferedReader to load file into memory. 
     this.br = new BufferedReader(this.fr); 
     System.out.println(br); 
     System.out.println("*****************************************************\n"); 

     // Read file contents a line at a time until file returns null. 
     while ((line = this.br.readLine()) != null) { 
      System.out.println(line); 

      // Let StringTokenizer parse each line and split data into elements 
      // using an empty space as the separator. 
      StringTokenizer stringTokenizer = new StringTokenizer(line, " "); 

      // Loop through each each element of the line 
      while (stringTokenizer.hasMoreElements()) { 
       this.address = Integer.parseInt(stringTokenizer.nextElement().toString()); 
       this.street = stringTokenizer.nextElement().toString(); 
       this.price = Double.parseDouble(stringTokenizer.nextElement().toString()); 
       this.rooms = Integer.parseInt(stringTokenizer.nextElement().toString()); 
       // Format price to look like money. 
       moneyFormat.format(this.price); 

       // Build a string just because I can 
       StringBuilder sb = new StringBuilder(); 
       sb.append("*******************"); 
       sb.append("\nAddress : ").append(this.address); 
       sb.append("\nStreet : ").append(this.street.toUpperCase()); 
       sb.append("\nPrice : ").append(this.price); 
       sb.append("\nRooms  : ").append(this.rooms); 
       sb.append("\n*******************"); 
       System.out.println(sb.toString()); 

       // Add data to array 
       houseList.add(new House(this.address, this.street, this.price, this.rooms)); 
      } 
     } 

     System.out.println("*****************************************************"); 
     System.out.println("Array is loaded"); 
     // Set a variable indicating array is loaded and ready for transactions. 
     //this.isArrayLoaded = true; 

    } catch (IOException e) { 
     // Catch any IO errors while accessing the file and alert the user. 
     System.out.println("An IO error occured while accessing " + fileName + ". Operation terminated.\n\n" + e); 
     //this.isArrayLoaded = false; 
    } finally { 
     try { 
      // Close file operations 
      if (this.br != null) { 
       this.br.close(); 
       this.fr.close(); 
      } 
     } catch (IOException ex) { 
      System.out.println("\n\n" + ex + "\n"); 
      ex.printStackTrace(); 
     } 
    } 
    System.out.println("\n\n"); 
    //return this.isArrayLoaded; 
} 

public void showHouses() { 
    Iterator<House> itr = houseList.iterator(); 
    while (itr.hasNext()) { 
     System.out.println(itr.next().toString()); 
    } 
} 
} 

這個類...

package housetracker; 

public class House { 
private int address; 
private String street; 
private double price; 
private int rooms; 

public House(int address, String street, double price, int rooms) { 
    this.address = address; 
    this.street = street; 
    this.price = price; 
    this.rooms = rooms; 
} 

public void SetAddress (int address){ 
    setAddress(address); 
} 

private void setAddress (int address){ 
    this.address = address; 
} 

public void SetStreet (String street){ 
    setStreet(street); 
} 

private void setStreet(String street){ 
    this.street = street; 
} 

public void SetPrice (double price){ 
    setPrice(price); 
} 

private void setPrice (double price){ 
    this.price = price; 
} 

public void SetRooms(int rooms){ 
    setRooms(rooms); 
} 

private void setRooms(int rooms){ 
    this.rooms = rooms; 
} 

public int GetAddress (int address){ 
    address = getAddress(address); 
    return address; 
} 

private int getAddress (int address){ 
    return address; 
} 

public String GetStreet (String street){ 
    street = getStreet(street); 
    return street; 
} 

private String getStreet(String street){ 
    return street; 
} 

public double GetPrice (double price){ 
    price = getPrice(price); 
    return price; 
} 

private double getPrice (double price){ 
    return price; 
} 

public int GetRooms(int rooms){ 
    rooms = getRooms(rooms); 
    return rooms; 
} 

private int getRooms(int rooms){ 
    return rooms; 
} 
@Override 
public String toString() { 
    return address + " " + street + " " + price + " " + rooms; 
} 
} 

+6

是與所有問題相關的代碼? – sjr

+0

@sjr我在質疑爲什麼代碼在沒有主要方法的情況下運行,所以我認爲是這樣。爲什麼只有一塊代碼?爲什麼不是全部?我發佈了一切,所以也許有人可以告訴我發生了什麼事情,爲什麼。你的評論如何對這篇文章做出貢獻? – Michael

+0

你有什麼嘗試?那麼你如何在塊運行之前放置一個塊?如何在這裏發佈整個代碼來說明問題? – sjr

回答

0

我可以在這裏一刪除?我發現一個隱藏在IDE中未打開的另一個類中的主要方法。我覺得自己像個白癡!