![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 3
![]() |
Multi array question
Just wanted to know if anyone will look over my code for me. The program is suppose to display a seating chart for an airplane:
1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7 A B C D It should then prompt the user for their seat choice and then replace their choice of seat with an "X" signaling that the seat is taken. Choice of "Q" exits the program. Having a problem displaying the seating chart though. Any suggestions? import java.util.*: //import the scanner class
public class Seating{
boolean taken = false; //seat intial value is untaken
public String SeatWarn(){
return "Seat Taken, please choose another seat." //warn if your seat is taken
}
public void seatTaken(int x, int y){ //change the value of a seat to taken when necessary
taken = true;
seats[x][y] = "X";
}
public static void main(String [] args){ //create and display the seating chart
int counter = 0;
int[] seats = new int[5][7];
int row, column;
for(row = 0; row < seats.length; row++){
for(column = 0; column < seats[row].length; column++){
System.out.println(seats[row][column]);
}
}
while (count < 36){ //while there are chairs left
Scanner kb = new Scanner(System.in);
System.out.prinln("Please choose a row to sit in:"); //prompt for a row
int rowChoice = kb.nextInt();
System.out.println("Please choose a seat to sit in: (""q"" to exit"); //prompt for a seat
char seatChoice = kb.nextChar();
if (seats[rowChoice][seatChoice].taken = true){ //update the seating chart to reflect seat choosen
SeatWarn();
}else{
seats[rowChoice][seatChoice].seatTaken(x,y);
} //Loop
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
You've got a ton of syntax errors. Run that code through a compiler so you can see them flagged. Also, you're going to run into some problems calling non-static methods from main.
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
make the array "seats" a static variable within the class(a global variable) instead of just having it in the main method.
public void seatTaken(int x, int y)
{ //change the value of a seat to taken when necessary
taken = true;
seats[x][y] = "X";
}EDIT: good point, Jaeusm!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 3
![]() |
thanks for the tips guys i did through it into the compiler and i started working on fixing the syntax.....sry about all the errors i'm just learning this language. I did have another question though. I wrote:
int[][] seats = new int[5][7]; and i get errors however: int [] [] seats; seats = new int[5][7]; compiles fine....why? i thought that they are the same statement just written two different ways? |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 3
![]() |
for(i = 0; i < 5; i++)
System.out.println(seat[i] + " " + seat[i]);this works to print two lines but that's as far as i'm getting with this...:-( |
|
|
|
|
|
#6 |
|
Sexy Programmer
|
what are you trying to do in that code?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#8 |
|
Sexy Programmer
|
DaWei, you just scared him away from this forum. lol
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If THAT scared him away, I'd recommend a trip to the balls store. It at least gives him something to look at and discover for himself a trouble spot. It also explains how 'for' loops work. What do you want? Egg in your beer? Or to just ask him what he's doing and give him nothing?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Sexy Programmer
|
I was confused in what he asked me so I asked him but then you gave him a paragraph on "expressing one self and clarity when asking a question".
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|