英文:
Java array declares as null
问题
import java.util.Scanner;
import java.util.Arrays;
public class Assignment8_jkaur1240026 {
public static void main(String args[]) {
int club_seat = 0;
int coach_seat = 5;
int counting_line1 = 0;
int counting_line2 = 0;
int counting_line3 = 0;
boolean reservation = true;
Scanner input = new Scanner(System.in);
do {
System.out.print("Please specify service class (1 = club 2 = coach): ");
int seating_class = input.nextInt();
System.out.println();
if (seating_class == 1) {
if (club_seat < 5) {
club_seat = club(club_seat);
System.out.println("Club Class: Seat #" + club_seat);
} else {
System.out.println("Sorry we are out of club seats");
}
} else if (seating_class == 2) {
if (coach_seat < 20) {
coach_seat = coach(coach_seat);
System.out.print("Coach Class: Seat #" + coach_seat);
} else {
System.out.println("Sorry we are out of coach seats");
}
} else {
System.out.println("Sorry we don't have that service class");
}
System.out.println();
System.out.print("Another reservation (1 = yes 2 = no)? ");
int another_reservation = input.nextInt();
if (another_reservation == 1) {
reservation = true;
} else if (another_reservation == 2) {
reservation = false;
System.out.println("Have a good day!");
} else {
System.out.println("Sorry we don't understand that command");
}
System.out.println();
} while (reservation == true);
String reservation_array_line1[][] = new String[20][1];
while (counting_line1 < 20) {
reservation_array_line1[counting_line1][0] = "Seat";
counting_line1 = counting_line1 + 1;
System.out.println(reservation_array_line1[counting_line1][0]);
}
int reservation_array_line2[][] = new int[20][1];
while (counting_line2 < 20) {
reservation_array_line2[counting_line2][0] = counting_line2;
counting_line2 = counting_line2 + 1;
System.out.println(reservation_array_line2[counting_line2][0]);
}
String reservation_array_line3[][] = new String[20][1];
while (counting_line3 < 20) {
reservation_array_line3[counting_line3][0] = "E";
counting_line3 = counting_line3 + 1;
System.out.println(reservation_array_line3[counting_line3][0]);
}
}
public static int club(int club_seat) {
club_seat = club_seat + 1;
return (club_seat);
}
public static int coach(int coach_seat) {
coach_seat = coach_seat + 1;
return (coach_seat);
}
}
英文:
I am having trouble with the code specifically with the arrays. I have a while loop set up for the arrays but it's declaring the arrays as null. Do you know what the bug is in my array section?
I'm not sure what do I need to do to fix the null issues in this case. I have went through the code several times and couldn't figure it out.
import java.util.Scanner;
//imports scanner object
import java.util.Arrays;
//imports scanner object
public class Assignment8_jkaur1240026 {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
int club_seat = 0;
//creates and initializes club_seat variable
int coach_seat = 5;
//creates and initializies coach_seat variable
int counting_line1 = 0;
//creates and initializes counting1 variable
int counting_line2 = 0;
//creates and initializes counting2 variable
int counting_line3 = 0;
//creates and initializes counting3 variable
boolean reservation = true;
//boolean used to keep the loop going
Scanner input = new Scanner( System.in );
// create a Scanner object
do{
System.out.print( "Please specify service class " );
System.out.println( "(1 = club 2 = coach): ");
//prompt the user to say club or coach seating
int seating_class = input.nextInt();
//stores the input in seating_class variable
System.out.println();
//line break
if(seating_class == 1){
//if seating_class var is 1
if(club_seat < 5){
//if club seat is less than 5
club_seat = club(club_seat);
System.out.println("Club Class: Seat # "+ club_seat);
//call the club method and prints it
}
else{
//if club seats are greater than 5
System.out.println("Sorry we are out of club seats");
}
}
else if(seating_class == 2){
//if seating_class var is 1
if(coach_seat < 20){
//if club seat is less than or equal to 20
coach_seat = coach(coach_seat);
System.out.print("Coach Class: Seat # "+ coach_seat);
//call the club method and prints it
}
else{
//if coach seats are greater than 20
System.out.println("Sorry we are out of coach seats");
}
}
else{
System.out.println("Sorry we don't have that service class");
//Say we don't have that class if seating class is not 1 or 2
}
System.out.println();
//line break
System.out.println("Another reservation (1 = yes 2 = no)? ");
//ask user if they want another reservation
int another_reservation = input.nextInt();
//store the value inside another reservation int
if(another_reservation == 1){
//if anther reservation is 1
reservation = true;
//keep the boolean reservation as true
}
else if(another_reservation == 2){
//if another reservation is 2
reservation = false;
//change the boolean reservation to false
System.out.println("Have a good day!");
//say bye bye
}
else{
//if another reservation int is not 1 or 2
System.out.println("Sorry we don't understand that command");
//tell the user that they are invalid response
}
System.out.println();
//line break
}
while(reservation == true);
//keep going if reservation is true
String reservation_array_line1[][] = new String [20][1];
while(counting_line1 < 20){
reservation_array_line1[counting_line1][0] = "Seat";
counting_line1 = counting_line1 + 1;
System.out.println(reservation_array_line1[counting_line1][0]);
}
int reservation_array_line2[][] = new int [20][1];
while(counting_line2 < 20){
reservation_array_line2[counting_line2][0] = counting_line2;
counting_line2 = counting_line2 + 1;
System.out.println(reservation_array_line2[counting_line2][0]);
}
String reservation_array_line3[][] = new String [20][1];
while(counting_line3 < 20){
reservation_array_line3[counting_line3][0] = "E";
counting_line3 = counting_line3 + 1;
System.out.println(reservation_array_line3[counting_line3][0]);
}
}
public static int club( int club_seat ){
//method to assign user club seats
club_seat = club_seat + 1;
//increase club_seat by one
return(club_seat);
//return that value
}
public static int coach( int coach_seat ){
//method to assign user coach seats
coach_seat = coach_seat + 1;
//increase coach_seat by one
return(coach_seat);
//return that value
}
}**
答案1
得分: 5
在每次循环中,您在访问当前元素之前先递增索引。例如,在第一个循环中,当counting_line1
为0时,您可能打算将reservation_array_line1[0][0]
设置为"Seat"
,然后打印出这个值,然后递增counting_line1
。
然而,因为您在访问之前递增,实际上会打印出reservation_array_line1[1][0]
,这个元素除了默认值null
之外什么也没有设置,因为这是您的循环的下一个迭代要处理的内容。实际上,在循环的最后一次迭代中,当尝试访问不存在的元素reservation_array_line1[20][0]
时,会出现ArrayIndexOutOfBoundsException错误。
要修复这个问题,只需交换递增和访问的顺序:
String reservation_array_line1[][] = new String [20][1];
while(counting_line1 < 20){
reservation_array_line1[counting_line1][0] = "Seat";
System.out.println(reservation_array_line1[counting_line1][0]);
counting_line1 = counting_line1 + 1;
}
int reservation_array_line2[][] = new int [20][1];
while(counting_line2 < 20){
reservation_array_line2[counting_line2][0] = counting_line2;
System.out.println(reservation_array_line2[counting_line2][0]);
counting_line2 = counting_line2 + 1;
}
String reservation_array_line3[][] = new String [20][1];
while(counting_line3 < 20){
reservation_array_line3[counting_line3][0] = "E";
System.out.println(reservation_array_line3[counting_line3][0]);
counting_line3 = counting_line3 + 1;
}
英文:
In each loop you increment the index prior to accessing the current element. For example, when counting_line1
is 0 in the first loop, you probably intend to set reservation_array_line1[0][0]
to "Seat"
, print out this value, and then increment counting_line1
.
However, because you increment before you access, you actually print out reservation_array_line1[1][0]
, which hasn't been set to anything except its default value, null
, because that's what the next iteration of your loop is supposed to take care of. In fact, you will notice an ArrayIndexOutOfBoundsException on the last iteration of your loop when a non-existent element, reservation_array_line1[20][0]
, attempts to be accessed.
To fix it, simply swap the order in which you increment and access:
String reservation_array_line1[][] = new String [20][1];
while(counting_line1 < 20){
reservation_array_line1[counting_line1][0] = "Seat";
System.out.println(reservation_array_line1[counting_line1][0]);
counting_line1 = counting_line1 + 1;
}
int reservation_array_line2[][] = new int [20][1];
while(counting_line2 < 20){
reservation_array_line2[counting_line2][0] = counting_line2;
System.out.println(reservation_array_line2[counting_line2][0]);
counting_line2 = counting_line2 + 1;
}
String reservation_array_line3[][] = new String [20][1];
while(counting_line3 < 20){
reservation_array_line3[counting_line3][0] = "E";
System.out.println(reservation_array_line3[counting_line3][0]);
counting_line3 = counting_line3 + 1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论