簡體   English   中英

需要幫助閱讀Java中的文件

[英]Need help reading a file in 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,位置:房間類別

對任何混亂的編碼或變量表示歉意

我想你的意思是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM