473,563 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2D Array newb

15 New Member
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 2261
Ganon11
3,652 Recognized Expert Specialist
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
The Midnighter
15 New Member
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
The Midnighter
15 New Member
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
3361
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 in the wrong direction. I want to count several strings of text in a file (security.txt) and output the counts with a brief description of each. ...
3
1395
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
36912
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; while (_dr.Read()) //loop through data reader to add items to the array
3
1528
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 grid. When I use: mBoard = New CBoardPosition (7,7)
0
1394
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 (class defined in the C# dll). I have a C++ executable which instantiates the C# DLL and invokes the API's. I am using SAFEARRAY to pass the array of...
27
2282
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 this array as an argument, is there a safe way to establish how many rows that are or should I iterate over a field I know will always be used and...
4
3433
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 know how to display the data on just the 12 row? thanks; Here is my code: Binding:
10
8440
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) { as_test = L"This is new data"; return 0; } c#
7
2921
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 way to do it? thanks!
0
7658
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7943
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6238
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.