gpt4 book ai didi

java 文件 readline 返回 null

转载 作者:行者123 更新时间:2023-12-01 23:38:18 25 4
gpt4 key购买 nike

我正在尝试用 Java 创建一个预订系统,但是每次运行该程序时,while 循环(如下所示)都会直接跳到末尾,就好像读取的行为空

   //hardcoded file path - needs to be changed when program moved
String fileName = "C:\\Users\\BOO\\Desktop\\SystemsSoftwareBookingsystem\\FilmData.txt";
String line = null;
int readInt = 0;
float readFloat = 0;
int item_counter = 0;

try
{

BufferedReader bufferedReaderF = new BufferedReader(new FileReader(new File(fileName)));

while ((line = bufferedReaderF.readLine()) != null)
{
Film tmpFilm = new Film();

switch (item_counter)
{
case 0:
{
line = bufferedReaderF.readLine();
tmpFilm.name = line;
item_counter++;
break;
}
case 1:
{
readInt = bufferedReaderF.read();
tmpFilm.seatsTotal = readInt;
item_counter++;
break;
}
case 2:
{
readInt = bufferedReaderF.read();
tmpFilm.seatsAvailable = readInt;
item_counter++;
break;
}
case 3:
{
readInt = bufferedReaderF.read();
tmpFilm.price = readFloat;
item_counter++;
break;
}
case 4:
{
readInt = bufferedReaderF.read();
tmpFilm.showingTime = readFloat;
item_counter++;
break;
}
case 5:
{
readInt = bufferedReaderF.read();
tmpFilm.day = readInt;
item_counter++;
break;
}
case 6:
{
readInt = bufferedReaderF.read();
tmpFilm.month = readInt;
item_counter++;
break;
}
case 7:
{
readInt = bufferedReaderF.read();
tmpFilm.year = readInt;
item_counter = 0;
break;
}
}

line = bufferedReaderF.readLine();

server.filmList.add(tmpFilm);

}

bufferedReaderF.close();

} catch (FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex)
{
System.out.println("Error reading file '" + fileName + "'");
}
}
}`

任何想法/帮助将不胜感激

编辑根据要求在 while 循环中添加了其余代码

编辑这里是我正在读取的文件

Film 1
10
10
5.00
10.30
Wednesday 23rd
July
2013

最佳答案

我不知道这是否与问题有关,但您需要在 switch 中的每个代码序列之后放置 break; 语句。否则,如果 item_counter 为 0,它将执行 0 的代码,然后失败并执行 1 的代码,然后执行 2 的代码,依此类推。

关于java 文件 readline 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18318310/

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