gpt4 book ai didi

java - 需要帮助读取 java 中的文件

转载 作者:行者123 更新时间:2023-12-01 13:14:06 25 4
gpt4 key购买 nike

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

这是我的主要类(class):

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("");
}
}

这是我的其他类(class):

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());

关于java - 需要帮助读取 java 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22598738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com