• 06-16-2011, 06:09 PM
fr0s1yjack
Hi all!!

im new to this so please have mercy!!

ok so i have a two dimensional array, that looks like this:

A B C D
1 0 0 0 0
2 0 0 0 0
3 etc
4 etc
5
6
i need to get the value of a specific 'cell'

here is what i have in my method so far:

Code:

```for (i = 0; i < 24; i++) {                 seats[i] = new Seat();                 int row = 1;                 String col = "";                 if(i%4 == 1){                     col = "A";                     }                 else if(i%4 == 2){                     col = "B";                   }                 else if(i%4 == 3){                     col = "C";                                     }                 else if(i%4 == 0){                     col = "D";                 }                                 seats[i].seatNo = row+col;                 row++;             }         }```
when i call it, i get the column fine, however im struggling to get the row value, i should mention i need the value to be 1A, 2C, 3D etc not 0!!

any help please guys i'd really appreciate it!!
• 06-16-2011, 06:19 PM
Norm
Quote:

im struggling to get the row value
Not sure what you are trying to do.
You post looks like you have labeled the columns with letters and the rows with numbers.
Quote:

i need the value to be 1A, 2C, 3D etc not 0!!
The contents (the cells???) are all zero.
Quote:

i need to get the value of a specific 'cell'
What is the "value"? the contents or the row, col?

What does your code output now?
Can you show what is wrong with the output and show what the output should be?
• 06-16-2011, 06:21 PM
fr0s1yjack
the output when i call

Code:

``` class.seats[3].getSeatNo();                             class.seats[10].getSeatNo();                             class.seats[20].getSeatNo();```
is

1C
1B
1D

its giving me the correct columns just not the right rows :S
• 06-16-2011, 06:22 PM
Norm
show what the output should be?
• 06-16-2011, 06:24 PM
fr0s1yjack
1C
3B
5D

i think, positions [3] [10] & [20] of the array
• 06-16-2011, 06:29 PM
Norm
Can you show the whole thing. You are tossing out bits and pieces. I have no idea how any of it fits together.

Try debugging your code by adding printlns to show the variable row as it changes. You need to detect why it is always = 1;
What conditions in your code keeps it at 1 or changes it to one before using it to build the seatNo?
• 06-16-2011, 06:35 PM
fr0s1yjack
Code:

```public class Flight {         protected Seat[] seats = new Seat[24];         String flightNumber;         String departure;         String arrival;           boolean available;         boolean checkingIn;         boolean boarding;         boolean closed;         boolean full;         static String statusMessage = "";         int freeSeats = 24;         int reservedSeats;         int bookedSeats;         int rows;         int cols;         int i;                 static Keyboard kb = new Keyboard();         String Date;                     Flight(){                         for (i = 0; i < 24; i++) {                 seats[i] = new Seat();                 int row = 1;                 String col = "";                 if(i%4 == 1){                     col = "A";                     }                 else if(i%4 == 2){                     col = "B";                   }                 else if(i%4 == 3){                     col = "C";                                     }                 else if(i%4 == 0){                     col = "D";                 }                                 seats[i].seatNo = row+col;                 row++;             }         }         public void setFlightDetails(String FlightNumber, String departure, String arrival, String Date){             this.flightNumber = FlightNumber;             this.departure = departure;             this.arrival = arrival;             this.Date= Date;         }                 public void viewSeat(){           System.out.println("      A B C D");                           int i=0;             for (rows = 0; rows < 6; rows++) {                 System.out.print("  " + (rows + 1) + " - ");                 for (cols = 0; cols  < 4; cols++) {                                         System.out.print(seats[i].status + " ");                   i++;                 }                 System.out.println("");             }                 kb.enterToContinue();         }```
class where the array is initialized:

Code:

```public class Seat  {  public Seat(){             for (int i = 0; i < 5; i++) {               // seatNo[i] = "";             } public void changeSeatStatus(){             Keyboard kb = new Keyboard();             currentStatus = kb.getInt("Enter Seat to Change Status: ");                 status = 1;         } }```
and the main class where i call the function:

public class Main {
static UserInterface ui = new UserInterface();
static Flight aFlight = new Flight();
static Keyboard kb = new Keyboard();
static Seat seat = new Seat();

public static void main(String[] args) {

Code:

```public static void bookings(){             ui.displayBookingsMenu();                     char menuChoice = 0;                 do{                   menuChoice = kb.getChar("select: ");                     if(menuChoice == '1')                           seat.changeSeatStatus();                     else if (menuChoice == '2')                           seat.changeSeatStatus();                     else if (menuChoice == '3')                             seat.changeSeatStatus();                     else if (menuChoice == '4'){                             aFlight.seats[3].getSeatNo();                             aFlight.seats[10].getSeatNo();                             aFlight.seats[20].getSeatNo();                     }               } while (menuChoice != 'R');         } }```
• 06-16-2011, 06:38 PM
Norm
Have you Tried debugging your code by adding printlns to show the variable row as it changes. You need to detect why it is always = 1;
What conditions in your code keeps it at 1 or changes it to one before using it to build the seatNo?
• 06-16-2011, 06:39 PM
fr0s1yjack
Code:

```Flight(){                         for (i = 0; i < 24; i++) {                 seats[i] = new Seat();   -------------> int row = 1;<-----```
whenever i change that value it will change the row number
• 06-16-2011, 06:51 PM
Norm
Quote:

whenever i change that value it will change the row number
Does it generate the correct row numbers for you?
It looks like it sets row to the value 1.
Isn't that what you were complaining about? That the row number was always 1.

Where do you change it?
• 06-16-2011, 06:52 PM
fr0s1yjack
thats what i cant figure out, the if statement handles the 'cols' by modulus, how do i also access the rows?
• 06-16-2011, 07:03 PM
Norm
Take a piece of paper and write out the elements of the single dimension array you are working with.
There appear to be 24 of them. So write down: 1 2 3 ... 24

Then write above each of those numbers from 1 to 24, its row and column.
Then look at the pattern and find an algorithm to convert the single number: 1-24 to the row and column you need.
• 06-17-2011, 02:21 AM
I'm a bit confused with your structure.

Lets say I have a Sudoku grid; it has 9 rows and 9 columns. Each Cell (row, col) has a value. You could represent it like so.

int[][] cells = new int[9][9];

ie
int row = 1;
int col = 2;

cells[row][col] = 3;

so we have row 2, col 2 equals 3.

hmm its quite late, but I can't see your 2D array at a glance.
• 06-17-2011, 03:32 AM
Junky
If I understand correctly you are trying to generate: 1A 1B 1C 1D 2A 2B 2C etc.

If so your problem is that you always increment row at the bottom of the loop and you always set it back to 1 at the top. Move the declaration of row outside of the loop and at the bottom you only want to increment row when col = 'D'.
• 06-23-2011, 02:37 PM
fr0s1yjack
thanks for the responses, i've found a way around it i think, however i'm still having a problem getting the correct position, heres what i have so far:

Code:

```public class Flight {         protected Seat[] seats = new Seat[32];         String flightNumber;         String departure;         String arrival;         String seatNumber = "";         boolean available;         boolean checkingIn;         boolean boarding;         boolean closed;         boolean full;         static String statusMessage = "";         int freeSeats = 24;         int reservedSeats;         int bookedSeats;         int rows;         int cols;         int i;         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));         static Keyboard kb = new Keyboard();         String Date;         // String line = "";         // line = read.readline();       // s = StringTokenizer(line, ':');         public  Flight(){                         for (i = 0; i < 32; i++) {                 seats[i] = new Seat();                 seats[i].seatNo = row+col;                 row++;             }             }     }                 public void setFlightDetails(String FlightNumber, String departure, String arrival, String Date){             this.flightNumber = FlightNumber;             this.departure = departure;             this.arrival = arrival;             this.Date= Date;                         }         public void getFlightdetails(){         }         public void setFlightStatus(){             statusMessage = (kb.getString("Enter Flight Status: "));         }         public void viewSeat(){           System.out.println("      A B C D");                           int i=0;             for (rows = 0; rows < 8; rows++) {                 System.out.print("  " + (rows + 1) + " - ");                 for (cols = 0; cols  < 4; cols++) {                                         System.out.print(seats[i].status + " ");                   i++;                 }                 System.out.println("");             }                 kb.enterToContinue();         }                           public void displayFlightInfo(){             System.out.println("Flight Number = " + flightNumber);             System.out.println("Daparture City = " + departure);             System.out.println("Arrival = " + arrival);             System.out.println("Date = " + Date);             System.out.println("Flight Status = " + statusMessage);                     }         public void displaySeatingPlan(){     }         public void loadFromFile(){         }         public void saveToFile(){         }         public void updateSeat(int type){             int r = 0;             int c = 0;             int position = 0;             int seatStatus = 0;             if(freeSeats == 0){             System.out.println("Flight " + flightNumber + " is full");             kb.enterToContinue();             return;         }else if (statusMessage.equals("Gate Closed")) {             System.out.println("Flight " + flightNumber + " has closed");             kb.enterToContinue();             return;         }             seatNumber = kb.getString("Please Enter Seat Number to Update: ");               try{             r = Integer.parseInt(String.valueOf(seatNumber.charAt(0)));             switch(seatNumber.charAt(1)){                 case 'A':case 'a':                     c = 1;                     break;                 case 'B':case 'b':                     c = 2;                     break;                 case 'C':case 'c':                     c = 3;                     break;                 case 'D':case 'd':                     c = 4;                     break;             }         } catch (Exception error) {                 System.out.println("INVALID SEAT NUMBER");                 kb.enterToContinue();                 return;         }             position = (((r - 1) * 4) + c) - 1;             seatStatus = Integer.parseInt(seats[position].seatNo[0]);             System.out.println(seatStatus);                 if(type == 0)             seats[position].displaySeatInfo(seatNumber, type, seatStatus);  //Display current seat status         else {             seats[position].changeSeatStatus(seatNumber, type, seatStatus);                       /* if(seats[position].isUpdated()){                 System.out.println("");                 System.out.println("Seat " + seatNumber + " has been successfully updated");             }else{                 System.out.println("");                 System.out.println("Seat " + seatNumber + " has NOT been successfully updated");             }*/         }         } }```
now the error im getting is:

Code:

```Exception in thread "main" java.lang.NumberFormatException: For input string: " "         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)         at java.lang.Integer.parseInt(Integer.java:449)         at java.lang.Integer.parseInt(Integer.java:499)         at scotiaAirlines.Flight.updateSeat(Flight.java:175)         at scotiaAirlines.Main.bookings(Main.java:121)         at scotiaAirlines.Main.main(Main.java:34) Java Result: 1```
i understand the error is also being passed in main, i can post the functions from main if you' would like?
• 06-23-2011, 02:43 PM
fr0s1yjack
oh!! and i just need an array, not a two dimensional array!!!!!
• 06-23-2011, 02:45 PM
Norm
Quote:

Looking thru the stack trace you posted, it looks like at line 175 in Flight you call parseInt with a String: " ".
The parseInt method wants valid numbers to work on so it throws the exception.
Your code should test if the String to be parsed is valid
Or it should catch the NumberFormatException and handle it.

For debugging print out the value of: seats[position].seatNo[0]
before trying passing it to parseInt()
• 06-23-2011, 03:13 PM
fr0s1yjack
im getting back '0'

the problem im having however is reading from a SINGLE array *slaps head*

I need the user to enter a seat as stated above as : 1A 1B 1C 1D 2A 2B 2C 2D 3A 3B......
and the seats position as in int would therefore be: 1 2 3 4 5 6 7 8 9 10............

looking at the code above how would i get this value?

for loop?
• 06-23-2011, 04:16 PM
Norm