Help with arrays in Java

NOBEL

Member
Aug 2, 2000
51
0
0
I am doing a GUI assignment, but I have trouble with arrays.
Basically what my program does is give you 8 plane seats you can choose, but once those 8 seats are full, the program will say "Plane full".
Trying to figure out how the array would work, I tried a small program to help me understand, but I am stuck, this probably very simple but can someone please help me.

Code is attached
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Logic:

You can mark each "seat" as empty or full using 1 or 0 (Could also be done using a boolean array)
When asked which "seat"'s are still empty, cycle through the array using a for loop and print each "seat" out like

System.out.println( "Seat " + i )

so that the user can tell you which point in the array (or "seat") they would like to take. If you get through the whole array without printing anything, the plane is full. This could be done using a boolean variable as a flag.
Then you could use that number like

seats[number]

to see if the seat is aleady taken, if it is, tell the user they can't have it and start over(Loop possibly?). Make sure that the number they enter is > 0 and < seats.length .




Also, I'm about 98.4% sure this doesn't work in Java

if(!(input= Nums[0],Nums[1], Nums[2]));

Maybe something like

if( input != Nums[0] || input != Numbs[1] || input != Numbs[2])

And by adding the semi colon after the statement, you have made it useless.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: amdfanboy
I'm about 99.3% sure this doesn't work in Java

if( input != Nums[0] || input != Numbs[1] || input != Numbs[2])

You of all people!! :shocked:

You can't do primitive comparison on Strings in java. It will work under special curcumstances but that's far beyond the scope of this code and is rarely ever used. To judge equality of Strings you use String.equals():

if (Nums[0].equals(input) || Nums[1].equals(input) || Nums[2].equals(input))...

You can just as easily do "input.equals(Num[x])". You just have to make sure that whichever one you use is guaranteed not to be null.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
Originally posted by: amdfanboy
I'm about 99.3% sure this doesn't work in Java

if( input != Nums[0] || input != Numbs[1] || input != Numbs[2])

You of all people!! :shocked:

You can't do primitive comparison on Strings in java. It will work under special curcumstances but that's far beyond the scope of this code and is rarely ever used. To judge equality of Strings you use String.equals():

if (Nums[0].equals(input) || Nums[1].equals(input) || Nums[2].equals(input))...

You can just as easily do "input.equals(Num[x])". You just have to make sure that whichever one you use is guaranteed not to be null.

LMAO, I didn't notice they were strings, I thought they were ints.:eek: