473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

2D Array newb

Alright, so basically here's what's supposed to happen.
It's an Airline reservation system, basically taking a row of 10 seats, 1 isle, 1 window. Yes, there's only one row. xD So 5 seats isle, 5 seats window.. Here's the code I have so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. package airlineconsole;
  3.  
  4. import java.io.*;
  5.  
  6. public class Main
  7. {
  8.     private static BufferedReader stdin = new BufferedReader(
  9.             new InputStreamReader( System.in ) );
  10.  
  11.     /** Creates a new instance of Main */
  12.     public Main()
  13.     {
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException
  17.     {
  18.         // Init
  19.         boolean[][] array = new boolean[5][5];
  20.         // end Init
  21.  
  22.         System.out.println("Welcome! Please enter the style seating(1 for Economy, 2 for First Class): ");
  23.         String seatStyle = stdin.readLine();
  24.         int seatStyleNum = Integer.parseInt( seatStyle );
  25.  
  26.         for (int i=0;i<5;i++)
  27.             for (int j=0;j<2;j++)
  28.             {
  29.                 while ((array[i][j]) == false)
  30.                 {
  31.                     System.out.println("Seat " +array[i][j]+ " Reserved.");
  32.                     array[i][j] = true;
  33.                 }
  34.             }
  35.     }
  36. }
  37.  
  38.  
My problem is, this goes through the entire array, and prints out EVERY seat. But I don't understand why it's doing that, since I set the value to TRUE after the fact?
Nov 6 '07 #1
3 2250
Ganon11
3,652 Expert 2GB
It's printing every seat because that's exactly what you are telling it to do. Your boolean array is initialized to all false values, and then, inside your loop (which checks every seat), it says, IF the current boolean value is false, THEN print this. Well, every place is false, so it prints every time.

BTW, your loop is ineffective here, as it will always execute once and ALWAYS once. You should just use an if statement.
Nov 6 '07 #2
Updated code:
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * Main.java
  3.  *
  4.  * Created on November 6, 2007, 12:56 PM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. package airlineconsole;
  11.  
  12. import java.io.*;
  13.  
  14. /**
  15.  *
  16.  * @author Administrator
  17.  */
  18. public class Main
  19. {
  20.     private static BufferedReader stdin = new BufferedReader(
  21.             new InputStreamReader( System.in ) );
  22.  
  23.     /** Creates a new instance of Main */
  24.     public Main()
  25.     {
  26.     }
  27.  
  28.     /**
  29.      * @param args the command line arguments
  30.      */
  31.     public static void main(String[] args) throws IOException
  32.     {
  33.         while(true)
  34.         {
  35.             // Init
  36.             int[][] array = new int[5][2];
  37.             boolean finished = false;
  38.             // end Init
  39.  
  40.             //System.out.println("Welcome! Please enter the style seating(1 for Economy, 2 for First Class): ");
  41.             System.out.println("1: Window seat\n2: Isle seat: ");
  42.             String seatStyle = stdin.readLine();
  43.             int seatStyleNum = Integer.parseInt( seatStyle );
  44.  
  45.             if(seatStyleNum == 1)
  46.             {
  47.                 for (int j=0;j<5;j++) // J Is the Window Seat
  48.                 {
  49.                     if (array[0][j]==0)
  50.                     {
  51.                         System.out.println("Window seat " +array[0][j]+ " reserved.");
  52.                         array[0][j] = 1;
  53.                         finished = true;
  54.                         break;
  55.                     }
  56.                     if (finished == true) break;
  57.                 }
  58.             }
  59.             else if (seatStyleNum == 2)
  60.             {
  61.                 for (int i = 0; i<5; i++) // I is the Isle seat
  62.                 {
  63.                     if (array[i][0] == 0)
  64.                     {
  65.                         System.out.println("Isle seat " +array[i][0]+ " reserved.");
  66.                         array[i][0] = 1;
  67.                         finished = true;
  68.                         break;
  69.                     }
  70.                     if (finished == true) break;
  71.                 }
  72.             }
  73.             else
  74.             {
  75.                 System.out.println("1 / 2 are the only valid options.");
  76.             }
  77.         }
  78.     }
  79. }
  80.  
Now, here's my NEW problem:

It's like, it's not filling the array with "1", because it keeps printing "Window seat 0 reserved"
Nov 6 '07 #3
A buddy of mine fixed the problem, had the initlization wrong >.< Was creating the array inside the loop geeez..........
Nov 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
3
by: madmike | last post by:
hey, first thanks for looking. I think this should be easy. I have a com DLL (in C++) that has this struct: struct IdxTimestampedWord { BSTR word; __int64 timestamp;
7
by: Cory Toms | last post by:
Hey All, I have question about the best way to go about doint this: SqlDataReader _dr=components.getItems(); fooclass _myarray = new fooclass; //create new array of my class int i=0; ...
3
by: HateSpam | last post by:
I am defining a class that has, as a member, an array of another user-defined class. private mBoard as CBoardPosition() The problem comes when I attempt to size the array, it should be an 8x8...
0
by: gchandran | last post by:
Hello, I am developing a DLL using C# . I am using VS 2005 for this. The component will expose few API's. One of the API performs certain operations and needs to return an array of objects...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
4
by: De_Kabal | last post by:
I'm trying to bind a 12x16 array to a repeater to display the information in a table to have certain format. Right now all it does is display all the array elements on individual rows. Does anyone...
10
by: ShadowLocke | last post by:
I am trying to pass a string array from c# into c++ and manipulate the data of that array. How can I do this? (I am c++ newb) c++ __declspec(dllexport) int __stdcall Test(LPCTSTR* as_test) {...
7
by: w1ck3d64 | last post by:
hi, i have an array of pixel information which i want to display with C/C++. I'm trying to use OpenCV at the moment but I don't have much experience writing pixel by pixel. Can someone show me a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.