Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2006, 1:22 PM   #1
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 4 NightShade01 is on a distinguished road
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

}
NightShade01 is offline   Reply With Quote
Old May 10th, 2006, 3:18 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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.
jaeusm is offline   Reply With Quote
Old May 10th, 2006, 3:23 PM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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";
}
This has no variable/array seats. I would recommend you reading about recursion. Google that, there are a lot of websites that have recursion and how to apply and understand it in Java.

EDIT: good point, Jaeusm!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old May 10th, 2006, 7:43 PM   #4
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 4 NightShade01 is on a distinguished road
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?
NightShade01 is offline   Reply With Quote
Old May 10th, 2006, 8:17 PM   #5
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 4 NightShade01 is on a distinguished road
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...:-(
NightShade01 is offline   Reply With Quote
Old May 10th, 2006, 9:20 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Originally Posted by NightShade01
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...:-(
Are you saying what you really mean to say? Express yourself clearly, the efficacy of your responses depends on it. Now, a simple question: in the print statement, why should "seat [i]" print anything different the second time, since you haven't changed either "seat" or "i" between the two uses. The way the for loop works is that i gets incremented at the end of the block, then back off up to the top you go, evaluate the conditional (i < 5), and either go again or move beyond the construct. If you want i to change anywhere other than at that point, write it in there. Your tools can't read your mind. Since you're a beginner, I recommend you use braces around your blocks, even when they aren't strictly required. Until you get some experience, you're going to put your ass in a crack.
__________________
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
DaWei is offline   Reply With Quote
Old May 10th, 2006, 9:13 PM   #7
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 10th, 2006, 10:35 PM   #8
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 10th, 2006, 10:51 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 10th, 2006, 11:05 PM   #10
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC