Monday, 10 October 2011

Day 12

so we're in quite a fix. here's how we spent 9 days out of the 18 we've been given to work on our project:
- Kisha went away to Turkey.
- Cole went away to Coorg.
- i had a zillion social obligations to fulfill.
- Nayak slept.
it's due this Friday aaaand this what we've done till now:
NOTHING.
(unless you count the random bits i've been doing)
but no more of that now. WE MUST WORK.
the others aren't back from their tuition yet so i've just started off...


while running my seat-booking program today, i noticed something.
when i print my loop showing the reserved seats, here's what comes up:

A1 A2 A3 A4 A5 A6 A7 A8 A10  B1 B2 B3 B4 B5 B6 B7 B8 B10 
C1 C2 C3 C4 C5 C6 C7 C8 C10  D1 D2 D3 D4 D5 D6 D7 D8 D10 
E1 E2 E3 E4 E5 E6 E7 E8 E10  F1 F2 F3 F4 F5 F6 F7 F8 F10 
G1 G2 G3 G4 G5 G6 G7 G8 G10  H1 H2 H3 H4 H5 H6 H7 H8 H10 
I1 I2 I3 I4 I5 I6 I7 I8 I10  J1 J2 J3 J4 J5 J6 J7 J8 J10 
K1 K2 K3 K4 K5 K6 K7 K8 K10  L1 L2 L3 L4 L5 L6 L7 L8 L10 
M1 M2 M3 M4 M5 M6 M7 M8 M10  N1 N2 N3 N4 N5 N6 N7 N8 N10 
O1 O2 O3 O4 O5 O6 O7 O8 O10  P1 P2 P3 P4 P5 P6 P7 P8 P10 
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q10  R1 R2 R3 R4 R5 R6 R7 R8 R10 
S1 S2 S3 S4 S5 S6 S7 S8 S10  T1 T2 T3 T4 T5 T6 T7 T8 T10 

i know, at first glance it seems fine. but i just noticed today - row 9 is missing. that also explains my NullPointerException - the last column in my array was empty.
now i must go about finding out where i went wrong.
ARGGGH!!! ><"
6.59am aim: FIGURE THIS OUT.
*******
7.39pm achievement
ohmygod the error was so, oh SO stupid! i can't believe i didn't spot it earlier!!
look at this bit:
for(char x='A'; x<='T'; x++)
  {for(char y='1'; y<='9'; y++)
   {seatAd+=x;
    seatAd+=y;
    seats[a][b]=seatAd;
    System.out.print(seatAd+"  ");
    seatAd="";
 if(y=='9') {System.out.print(x+"10\t\t"); seats[a][b]=(x+"10");}
    b++; if(b==9) b=0;
}
   counter++;
   if(counter==2) {System.out.println(); counter=0;}
   a++;
  }
 }
what's happening here is that, when y is 9, x+10 is being displayed and in my array x+10 is being stored in the same address x+9 was stored in. so technically, x+10 is replacing x+9!
okay, maybe that didn't make a lot of sense. i'll elaborate. 
let's look at b and y in our loop. 
when y is 1, b is 0.
         y = 2, b = 1. 
         y = 3, b = 2. 
         y = 4, b = 3. 
         y = 5, b = 4. 
         y = 6, b = 5. 
         y = 7, b = 6. 
         y = 8, b = 7. 
         y = 9, b = 8.
i've said, if y==9, i print x+10 and seat[a][b]=x+10. since b is STILL 8 x+10 replaces x+9.
yeah, it may seem a little complicated, but really, i just need to add two lines to fix this. not even two lines - one statement and one keyword. b++; and an "else". here's the edited code. notice, it's back to being b<10 and y<10.
import java.io.*;
public class Seats
{static String seatAd="", seatChoice="", seats[][]=new String[20][10];
 static int seatQty, counter=0, a=0, b=0;
 public static void seatQtyDisplay()throws IOException
 {System.out.println("Enter no. of seats, please. (:");
  seatQty=Integer.parseInt(new DataInputStream(System.in).readLine());
  System.out.println("Here is the layout of seats.");
  for(char x='A'; x<='T'; x++)
  {for(char y='1'; y<='9'; y++)
   {seatAd+=x;
    seatAd+=y;
    seats[a][b]=seatAd;
    System.out.print(seatAd+"  ");
    seatAd="";
    if(y=='9') {System.out.print(x+"10\t\t"); b++; seats[a][b]=(x+"10"); b=0;}
    else b++;
   }
   counter++;
   if(counter==2) {System.out.println(); counter=0;}
   a++;
  }
  counter=0;
 }
 public static void seatBook()throws IOException
 {System.out.println("To select seat(s), enter the seat no(s).");
  System.out.println("Note: Seats marked x have been booked already.");
  System.out.println("Ex.:If you wish to pick A5 & A6 - type A5, press Enter. Then type A6 and press Enter.");
  for(int x=0; x<seatQty; x++)
  {seatChoice=new DataInputStream(System.in).readLine();
   for(int a=0; a<20; a++)
   {for(int b=0; b<10; b++)
    {if(seats[a][b].equals(seatChoice)) {seats[a][b]="x "; break;}
    }
   }
  }
  System.out.println("Your seat(s) have been reserved.");
  for(int x=0; x<20; x++)
  {for(int y=0; y<10; y++)
   {System.out.print(seats[x][y]+" ");}
   counter++; if(counter==2) {System.out.println(); counter=0;}
  }
 }
 public static void main(String[] args)throws IOException
 {seatQtyDisplay();
  seatBook();
 }
}
 
    
and with that i've finished my bit for now.

No comments:

Related Posts Plugin for WordPress, Blogger...