Jump to content

2D Array newb

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
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?

#2
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
Alright, I'm not going to edit my old post because I want people to be able to see the complete code for that, but I'm not going to make a new thread because it's the same program as this one.. Anyway,

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"... Doesn't array[i][0] = 1; put 1 there?

#3
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
A buddy of mine fixed the problem, had the initlization wrong >.< Was creating the array inside the loop geeez..........

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Glad you could get it fixed. :)