java - 读取数据并存储在数组Java中
问题描述
我正在编写一个程序,允许用户在酒店预订房间(大学项目)。我遇到了这个问题,当我尝试从文件中读取数据并将其存储在一个数组中时,我收到一个NumberFormatException。
我已经在这个问题上停留了一段时间,无法弄清楚我哪里出错了。我已经阅读了它,显然是当我尝试将字符串转换为数字时,但我无法弄清楚如何修复它。
请问有什么建议吗?
这是我的读者代码。
FileReader file = new FileReader("rooms.txt");
Scanner reader = new Scanner(file);
int index = 0;
while(reader.hasNext()) {
int RoomNum = Integer.parseInt(reader.nextLine());
String Type = reader.nextLine();
double Price = Double.parseDouble(reader.nextLine());
boolean Balcony = Boolean.parseBoolean(reader.nextLine());
boolean Lounge = Boolean.parseBoolean(reader.nextLine());
String Reserved = reader.nextLine();
rooms[index] = new Room(RoomNum, Type, Price, Balcony, Lounge, Reserved);
index++;
}
reader.close();
这是我试图读取的文件中的数据:
解决方案
像这样改变你的while循环
while (reader.hasNextLine())
{
// then split reader.nextLine() data using .split() function
// and store it in string array
// after that you can extract data from the array and do whatever you want
}
推荐阅读
- rest - Delphi REST 调试器返回错误 429 Too Many Requests 但浏览器按预期返回 JSON
- python - 无法使用 Selenium 和 Python 定位输入框元素
- javascript - 当我将操作传递给 mapDispatchToProps 时,为什么 this.props 未定义?
- reactjs - 使用 react-material-ui-carousel 包含背景图片
- c++ - 使用宏的扩展结果作为其他宏的参数
- css - 解释 CSS 属性的使用:-webkit-border-radius 和border-radius
- python - 输入字符串后如何删除列表项?
- arrays - 使用 Dynamoose 将 LIST (ARRAY) 中的元素删除到 DynamoDB
- javascript - reactjs 从 cognito 获取参数
- python - Python函数重新分配外部列表值