繁体   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