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: -
-
package airlineconsole;
-
-
import java.io.*;
-
-
public class Main
-
{
-
private static BufferedReader stdin = new BufferedReader(
-
new InputStreamReader( System.in ) );
-
-
/** Creates a new instance of Main */
-
public Main()
-
{
-
}
-
-
public static void main(String[] args) throws IOException
-
{
-
// Init
-
boolean[][] array = new boolean[5][5];
-
// end Init
-
-
System.out.println("Welcome! Please enter the style seating(1 for Economy, 2 for First Class): ");
-
String seatStyle = stdin.readLine();
-
int seatStyleNum = Integer.parseInt( seatStyle );
-
-
for (int i=0;i<5;i++)
-
for (int j=0;j<2;j++)
-
{
-
while ((array[i][j]) == false)
-
{
-
System.out.println("Seat " +array[i][j]+ " Reserved.");
-
array[i][j] = true;
-
}
-
}
-
}
-
}
-
-
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?
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.
Updated code: - /*
-
* Main.java
-
*
-
* Created on November 6, 2007, 12:56 PM
-
*
-
* To change this template, choose Tools | Template Manager
-
* and open the template in the editor.
-
*/
-
-
package airlineconsole;
-
-
import java.io.*;
-
-
/**
-
*
-
* @author Administrator
-
*/
-
public class Main
-
{
-
private static BufferedReader stdin = new BufferedReader(
-
new InputStreamReader( System.in ) );
-
-
/** Creates a new instance of Main */
-
public Main()
-
{
-
}
-
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String[] args) throws IOException
-
{
-
while(true)
-
{
-
// Init
-
int[][] array = new int[5][2];
-
boolean finished = false;
-
// end Init
-
-
//System.out.println("Welcome! Please enter the style seating(1 for Economy, 2 for First Class): ");
-
System.out.println("1: Window seat\n2: Isle seat: ");
-
String seatStyle = stdin.readLine();
-
int seatStyleNum = Integer.parseInt( seatStyle );
-
-
if(seatStyleNum == 1)
-
{
-
for (int j=0;j<5;j++) // J Is the Window Seat
-
{
-
if (array[0][j]==0)
-
{
-
System.out.println("Window seat " +array[0][j]+ " reserved.");
-
array[0][j] = 1;
-
finished = true;
-
break;
-
}
-
if (finished == true) break;
-
}
-
}
-
else if (seatStyleNum == 2)
-
{
-
for (int i = 0; i<5; i++) // I is the Isle seat
-
{
-
if (array[i][0] == 0)
-
{
-
System.out.println("Isle seat " +array[i][0]+ " reserved.");
-
array[i][0] = 1;
-
finished = true;
-
break;
-
}
-
if (finished == true) break;
-
}
-
}
-
else
-
{
-
System.out.println("1 / 2 are the only valid options.");
-
}
-
}
-
}
-
}
-
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"
A buddy of mine fixed the problem, had the initlization wrong >.< Was creating the array inside the loop geeez..........
Sign in to post your reply or Sign up for a free account.
Similar topics |
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. ...
|
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;
|
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
|
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)
|
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...
| |
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...
|
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:
|
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#
|
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!
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |