这是我目前拥有的代码,但它不起作用,如果要完成这部分课程,请寻求帮助。提前致谢!
private static void reserveSeat() throws FileNotFoundException {
Scanner newFile = new Scanner(new FileReader("C:\\Users\\sylve\\OneDrive\\Desktop\\seats2.txt"));
int index = 0;
while (newFile.hasNext()) {
String seatsData = newFile.nextLine();
String[] seatData = seatsData.split(" ");
String seatNumber = seatData[0];
String seatClass = seatData[1];
boolean isWindow = Boolean.parseBoolean(seatData[2]);
boolean isAisle = Boolean.parseBoolean(seatData[3]);
boolean isTable = Boolean.parseBoolean(seatData[4]);
double seatPrice = Double.parseDouble(seatData[5]);
String email = seatData[6];
seats[index] = new seatBookingSystem(seatNumber , seatClass , isWindow , isAisle , isTable , seatPrice , email);
index++;
}
newFile.close();
System.out.println("Would you like a STD or a 1st seat? ");
String classWanted = input.next().toUpperCase();
System.out.println("would you like a Aisle or a window? ");
String typeWanted = input.next().toUpperCase();
boolean inputWindow = typeWanted.equals("WINDOW");
System.out.println("Would you like a table seat, Yes or No? ");
String tableWanted = input.next().toUpperCase();
boolean inputTable = tableWanted.equals("YES");
Object[] results = new Object[] {null, classWanted, inputWindow, null, inputTable, null, null};
if (seats[index] == results[0]) {
System.out.println(Arrays.toString(seats));
}
else {
System.out.println("-- Sorry there are no seats avilable! Please try again! --");
我已经尝试搜索几乎所有我能想到的东西并尝试使用它,但我无法弄清楚。
我正在读入的文件和格式是;座位数座位类别是窗口是过道是表座位价格电子邮件。
1A STD true false false 23.50 free
1B STD false true false 23.50 free
1D STD true true false 27.50 free
2A STD true false true 24.50 free
2B STD false true true 24.50 free
2D STD true true true 28.50 free
3A STD true false true 24.50 free
3B STD false true true 24.50 free
3D STD true true true 28.50 free
4A STD true false false 23.50 free
4B STD false true false 23.50 free
4D STD true true false 27.50 free
5A 1ST true true true 48.50 free
5C 1ST false true true 44.50 free
5D 1ST true false true 44.50 free
6A 1ST true true true 48.50 free
6C 1ST false true true 44.50 free
6D 1ST true false true 44.50 free
让我们从您编写的代码后退一步,看看您想要做什么:
现在看看你的代码做了什么:
您可以很好地使用while
循环逐行读取文件,使用string.Split
一些parse
方法将行分解为所需的值,然后将每个座位组织在一个数组中(即在其他地方声明,但我假设只要数组足够大,那部分仍然可以工作;不过我建议使用局部ArrayList<seatBookingSystem>
变量)。
您在询问用户方面做得很好(尽管就可用性而言,提供有关输入内容的更好说明是有意义的,并确保例如如果输入不是“WINDOW”,它实际上是“AISLE”等;但是让我们暂时忽略它)
但是现在出现问题的部分是:您将用户的标准放在一个Object
数组中。让我们看看你现在拥有什么:
seats:
type: seatBookingSystem[]
length: 18
entries:
0: seatBookingSystem
1: seatBookingSystem
2: seatBookingSystem
...
17: seatBookingSystem
results:
type: Object[]
length: 7
entries:
0: <null>
1: String as Object
2: boolean auto-boxed in Boolean as Object
3: <null>
4: boolean auto-boxed in Boolean as Object
5: <null>
6: <null>
如您所见,results
是属性列表,而seats
是具有属性的座位列表。将各种变量组织在一个数组中Object
并因此失去类型安全性并没有任何好处。因此,我会完全取消results
数组并坚持使用您已有的局部变量。如果您真的坚持在一个对象中组织所有属性,要么创建一个强类型容器类(如您的seatBookingSystem
),要么使用seatBookingSystem
它自己并为您不关心的值提供虚拟值。
最后,您将需要第二个循环。这是您目前错过的关键部分。您有第一个循环来填充数组,现在您需要第二个循环来遍历填充的数组并找到匹配的数组。您可以使用for
循环增加您当前正在检查的元素的索引变量
for (int i = 0; i < index; ++i) {
seatBookingSystem current = seats[i];
// TODO: expand expression to test for the other properties
if (current.getSeatClass().equals(classWanted)) {
System.out.println("Matching seat: " + current.getSeatNumber() + ", " +
"Price: " + current.getSeatPrice());
}
}
或使用for-each
循环
for(seatBookingSystem current : seats) {
// if ( ...
}
为了使for-each
循环正常工作,您必须确保数组的尺寸正确。它目前似乎具有固定大小,因此当文件中的行数少于seats
数组的长度时,您将获得null
数组末尾空槽的一些值。这就是前面提到的ArrayList<seatBookingSystem>
会派上用场的地方,它总是根据它当前拥有的元素数量自动调整大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句