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;
}
}

4

2 回答 2

3

奇怪的是,是的:Java 的早期版本似乎允许您运行没有“main()”的应用程序。

这种行为现在在 JDK 7 中是不允许的 :)

于 2012-11-13T21:45:55.733 回答
0

我可以在这里删除吗?我发现隐藏在我的 IDE 中未打开的另一个类中的主要方法。我觉得自己像个白痴!

于 2012-11-13T22:41:26.540 回答