需要帮助在java中读取文件

时间:2014-03-23 23:50:11

标签: java

我正在尝试让我的代码读取文件。我可以存储一个文件,但我不知道如何修复我收到的读取文件的错误。

这是我的主要课程:

public static void main(String[] args) throws IOException {
    Scanner input = new Scanner(System.in);
    Room[] myHotel = new Room[6];
    for (int x = 0; x < myHotel.length; x++) {
        myHotel[x] = new Room();
    }
    String roomName = null;
    String choice;
    String emptyRoom = "empty";
    int roomNum = 0;
    initialise(myHotel);
    while ( roomNum < 6 )
    {
        System.out.println("V: To View rooms");
        System.out.println("A: To Move customer to room");
        System.out.println("D: To Remove customer from room");
        System.out.println("S: Store data into text file");
        System.out.println("L: Read data from file");


        choice = input.next();

        if (choice.equals("V")) //views all the rooms 
        {   
            view(myHotel, roomName);     
        }
        if (choice.equals("A"))
        { 
            System.out.println("Enter room number (0-5) or 6 to stop:" ); 
            roomNum = input.nextInt(); 
            System.out.println("Enter the name for the room " + roomNum + " : " ) ; 
            roomName = input.next(); 
            myHotel[roomNum].setName(roomName);
            add(myHotel, roomName);
            System.out.println(" ");
        }
        if (choice.equals("D"))
        {
            view(myHotel, roomName);
            System.out.println("Enter room you want to remove a customer from: ");
            roomNum = input.nextInt();
            myHotel[roomNum].setName(emptyRoom);
            delete(myHotel, roomName);
            System.out.println("");
        }
        if (choice.equals("S"))
        {
            store(myHotel);
        }
        if (choice.equals("L"))
        {
            System.out.println("");
            load(myHotel);
        }
    }
}
    private static void initialise( Room hotelRef[] ) {
        for (int x = 0; x < 6; x++ ) hotelRef[x].getName();
    System.out.println( "initilise ");
    }
public static void view(Room[] myHotel, String roomName){

    System.out.println("All the rooms are shown below:");

    for (int x = 0; x < 6; x++)
    {   
        System.out.println("room " + x + " occupied by " + myHotel[x].getName());
    }
}
private static void add(Room[] myHotel, String roomName){
    for (int x = 0; x < 6; x++)
    {
        System.out.println("room " + x + " is occupied by " + myHotel[x].getName());
    }
}
 private static void delete(Room[] myHotel, String roomName){
    for (int x = 0; x < 6; x++ )
    {
        System.out.println("room " + x + " occupied by " + myHotel[x].getName());
    }
}
 private static void store(Room myHotel[]) throws IOException{

    BufferedWriter writer = null;
    try {

    writer = new BufferedWriter(new FileWriter("myhotel.txt"));
    for ( int x = 0; x < 6; x++)
    {      
      writer.write(myHotel[x].getName());
      writer.newLine();
      writer.flush();
    }

} catch(IOException ex) {
    ex.printStackTrace();
}
    System.out.println("You have stored the text file");
    System.out.println("");
}
 private static void load(Room myHotel[]) throws IOException{

 BufferedReader reader;

 try {
     reader = new BufferedReader(new FileReader("myhotel.txt"));   

     for ( int x = 0; x < 6; x++){

         myHotel[x].getName = reader.readLine(); //Receiving error here
     }
     } catch(IOException ex) {
    ex.printStackTrace();
     }
 System.out.println("You have loaded the text file");
 System.out.println("");
 } 
}

这是我的另一堂课:

public class Room {

    private String mainName;
    int guestsInRoom;

    public Room() {
        mainName = "empty ";
        System.out.println(" empty rooms ");
    }

    public void setName(String aName) {
        System.out.println("You have moved a customer to a room ");
        System.out.println("");
        mainName = aName;
    }

    public String getName() {
        return mainName;
    }
}

我不知道如何解决这个问题 - myHotel [x] .getName = reader.readLine(); 我试过myHotel [x] .setName = reader.readLine();甚至在寻求帮助之前,我仍然收到与getName相同的错误。

我收到错误找不到符号,符号:变量getName,位置:Room Class

为任何混乱的编码或变量道歉

1 个答案:

答案 0 :(得分:1)

我认为你的意思是:

myHotel[x].setName(reader.readLine());