I have these 2 classes:

import javax.swing.JOptionPane;


public class Theatre2D {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// declares an array of integers
        int[][] myTheatre2D;

        // allocates memory for 2*10 integers
        myTheatre2D = new int[2][10];
           
        myTheatre2D[0][0] = 20;
        myTheatre2D[0][1] = 30;
        myTheatre2D[0][2] = 40;
        myTheatre2D[0][3] = 50;
        myTheatre2D[0][4] = 60;
        myTheatre2D[0][5] = 60;
        myTheatre2D[0][6] = 50;
        myTheatre2D[0][7] = 40;
        myTheatre2D[0][8] = 30;
        myTheatre2D[0][9] = 20;
        //Start Second Row
        myTheatre2D[1][0] = 20;
        myTheatre2D[1][1] = 30;
        myTheatre2D[1][2] = 40;
        myTheatre2D[1][3] = 50;
        myTheatre2D[1][4] = 60;
        myTheatre2D[1][5] = 60;
        myTheatre2D[1][6] = 50;
        myTheatre2D[1][7] = 40;
        myTheatre2D[1][8] = 30;
        myTheatre2D[1][9] = 20;
	
        String[][] seatStatus = new String[2][10];
        seatStatus[0][0] = "Open";
        seatStatus[0][1] = "Open";
        seatStatus[0][2] = "Open";
        seatStatus[0][3] = "Open";
        seatStatus[0][4] = "Open";
        seatStatus[0][5] = "Open";
        seatStatus[0][6] = "Open";
        seatStatus[0][7] = "Open";
        seatStatus[0][8] = "Open";
        seatStatus[0][9] = "Open";
        //Second row
        seatStatus[1][0] = "Open";
        seatStatus[1][1] = "Open";
        seatStatus[1][2] = "Open";
        seatStatus[1][3] = "Open";
        seatStatus[1][4] = "Open";
        seatStatus[1][5] = "Open";
        seatStatus[1][6] = "Open";
        seatStatus[1][7] = "Open";
        seatStatus[1][8] = "Open";
        seatStatus[1][9] = "Open";
        

    	int column = 0;
    	int row = 0;
    	while (row<2){
    		while (column<10){
    		System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
    				+myTheatre2D[row][column] +" dollars."
    				+ "That seat is currently " + seatStatus[row][column]);
    		column+=1;
    		}
    		row+=1;
    		column = 0;
    }
    

        
        for (int s=0;s<20;s++){
        	
        	String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
			int crownum = Integer.parseInt(crow);
        	String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
			int cseatnum = Integer.parseInt(cseat);
			
			if (seatStatus[crownum-1][cseatnum-1]=="Open");
				seatStatus[crownum-1][cseatnum-1] = "Occupied";
				System.out.println("");
				System.out.println("Thanks for you purchase! It has cost you $"+myTheatre2D[crownum-1][cseatnum-1]+".");
				System.out.println("");
				
				myTheatre.PrintSeats();

        	
        }

		
	}

}

and

public class Seating2D {
	public void PrintSeats() {
		int[][] seats = new int[10][5];
		int column = 0;
		int row = 0;
		while (row<5){
			while (column<10){
			System.out.println(seats[column][row]);
			column+=1;
			}
			row+=1;
			column = 0;
	}
	}
}

I am having trouble making the main class use the PrintSeats module.Any help is appreciated.

Thanks,


Jaro

Hi. First off, I would suggest to use, on line 86,

if (seatStatus[crownum-1][cseatnum-1].equals("Open"))

Also, put everything you want that IF statement (the one I talked about above) to do in curly brackets. As of now, on the code here, the only statement that the IF is executing is the one directly below it. So, stick all of that in a pair of brackets.
Now, onto your problem. What I would do would be to just tack that method onto the end of your main file, instead of in a separate class. You would, of course, have to add in the keyword static, as shown below:

import javax.swing.JOptionPane;


public class Theatre2D {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// ... main method stuff ...
	}

        public static void PrintSeats() {
		// ... other stuff ...
	}

}

Then, you just call it like PrintSeats(); and that should work. Try it out, and let me know how it goes for you.

I tried putting it on the same class however I encountered a new problem. It tells me that "printSeats is undefined for type myTheatre2D". I made a few adjustments to the code below and my question is how can I get it to print a revised list everytime a new seat is purchased?

import javax.swing.JOptionPane;


public class Theatre2D {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// declares an array of integers
        int[][] myTheatre2D;

        // allocates memory for 2*10 integers
        myTheatre2D = new int[2][10];
           
        myTheatre2D[0][0] = 20;
        myTheatre2D[0][1] = 30;
        myTheatre2D[0][2] = 40;
        myTheatre2D[0][3] = 50;
        myTheatre2D[0][4] = 60;
        myTheatre2D[0][5] = 60;
        myTheatre2D[0][6] = 50;
        myTheatre2D[0][7] = 40;
        myTheatre2D[0][8] = 30;
        myTheatre2D[0][9] = 20;
        //Start Second Row
        myTheatre2D[1][0] = 20;
        myTheatre2D[1][1] = 30;
        myTheatre2D[1][2] = 40;
        myTheatre2D[1][3] = 50;
        myTheatre2D[1][4] = 60;
        myTheatre2D[1][5] = 60;
        myTheatre2D[1][6] = 50;
        myTheatre2D[1][7] = 40;
        myTheatre2D[1][8] = 30;
        myTheatre2D[1][9] = 20;
	
        String[][] seatStatus = new String[2][10];
        seatStatus[0][0] = "Open";
        seatStatus[0][1] = "Open";
        seatStatus[0][2] = "Open";
        seatStatus[0][3] = "Open";
        seatStatus[0][4] = "Open";
        seatStatus[0][5] = "Open";
        seatStatus[0][6] = "Open";
        seatStatus[0][7] = "Open";
        seatStatus[0][8] = "Open";
        seatStatus[0][9] = "Open";
        //Second row
        seatStatus[1][0] = "Open";
        seatStatus[1][1] = "Open";
        seatStatus[1][2] = "Open";
        seatStatus[1][3] = "Open";
        seatStatus[1][4] = "Open";
        seatStatus[1][5] = "Open";
        seatStatus[1][6] = "Open";
        seatStatus[1][7] = "Open";
        seatStatus[1][8] = "Open";
        seatStatus[1][9] = "Open";
        
        printSeats();

        for (int s=0;s<20;s++){
        	
        	String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
			int crownum = Integer.parseInt(crow);
        	String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
			int cseatnum = Integer.parseInt(cseat);
			
			if (seatStatus[crownum-1][cseatnum-1].equals("Open"));
				seatStatus[crownum-1][cseatnum-1] = "Occupied";
				System.out.println("");
				System.out.println("Thanks for you purchase! It has cost you $"+myTheatre2D[crownum-1][cseatnum-1]+".");
				System.out.println("");
				
				printSeats();
		    }
        }

	
	public static void PrintSeats() {
    	int column = 0;
    	int row = 0;
    	while (row<2){
    		while (column<10){
    		System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
    				+myTheatre2D[row][column] +" dollars."
    				+ "That seat is currently " + seatStatus[row][column]);
    		column+=1;
    		}
    		row+=1;
    		column = 0;
    }
}
}

In the if statement, it should read 'PrintSeats();', not 'printSeats();'. Java is case sensitive. See if fixing that fixes your problem.

I fixed the error in the spelling however I still get an error in the System.out.println() it says that myTheatre2D and seatStatus cannot be resolved. Is there any way to make it so that those lists are global such as is done in python and other languages.

I just spoke with my teacher, he said it had to do with setting up the public constructor. Not too sure how I should go about this...

The way to make your myTheatre2D global is to put its declaration before the main method, like below:

private int[][] myTheatre2D;

public static void main(String[] args){/*...code here*/}

You needn't actually allocate the space for the array before main, just make sure that it is declared before the main method to make it global. Let me know if you still have problems.

I found a way around making the new method in the following code:

import javax.swing.JOptionPane;


public class Theatre2D {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// declares an array of integers
        int[][] myTheatre2D;

        // allocates memory for 2*10 integers
        myTheatre2D = new int[2][10];
           
        myTheatre2D[0][0] = 30;
        myTheatre2D[0][1] = 40;
        myTheatre2D[0][2] = 50;
        myTheatre2D[0][3] = 60;
        myTheatre2D[0][4] = 70;
        myTheatre2D[0][5] = 70;
        myTheatre2D[0][6] = 60;
        myTheatre2D[0][7] = 50;
        myTheatre2D[0][8] = 40;
        myTheatre2D[0][9] = 30;
        //Start Second Row
        myTheatre2D[1][0] = 20;
        myTheatre2D[1][1] = 30;
        myTheatre2D[1][2] = 40;
        myTheatre2D[1][3] = 50;
        myTheatre2D[1][4] = 60;
        myTheatre2D[1][5] = 60;
        myTheatre2D[1][6] = 50;
        myTheatre2D[1][7] = 40;
        myTheatre2D[1][8] = 30;
        myTheatre2D[1][9] = 20;
	
        String[][] seatStatus = new String[2][10];
        seatStatus[0][0] = "Open";
        seatStatus[0][1] = "Open";
        seatStatus[0][2] = "Open";
        seatStatus[0][3] = "Open";
        seatStatus[0][4] = "Open";
        seatStatus[0][5] = "Open";
        seatStatus[0][6] = "Open";
        seatStatus[0][7] = "Open";
        seatStatus[0][8] = "Open";
        seatStatus[0][9] = "Open";
        //Second row
        seatStatus[1][0] = "Open";
        seatStatus[1][1] = "Open";
        seatStatus[1][2] = "Open";
        seatStatus[1][3] = "Open";
        seatStatus[1][4] = "Open";
        seatStatus[1][5] = "Open";
        seatStatus[1][6] = "Open";
        seatStatus[1][7] = "Open";
        seatStatus[1][8] = "Open";
        seatStatus[1][9] = "Open";
        
    	int column = 0;
    	int row = 0;
    	while (row<2){
    		while (column<10){
    		System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
    				+myTheatre2D[row][column] +" dollars."
    				+ "That seat is currently " + seatStatus[row][column]);
    		column+=1;
    		}
    		row+=1;
    		column = 0;
    	}
    	int bookcounter = 0;
        while (bookcounter<20){
        	try{
        	String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
			int crownum = Integer.parseInt(crow);
        	String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
			int cseatnum = Integer.parseInt(cseat);
			
			if (seatStatus[crownum-1][cseatnum-1].equals("Open")){
				seatStatus[crownum-1][cseatnum-1] = "Occupied";
				System.out.println("");
				System.out.println("Thanks for you purchase! It has cost you $"+myTheatre2D[crownum-1][cseatnum-1]+".");
				System.out.println("");
				bookcounter+=1;
			}
			else if(seatStatus[crownum-1][cseatnum-1].equals("Occupied")){
				System.out.println("");
				System.out.println("Unfortunately that seat has been taken. Please chose another seat.");		
				System.out.println("");
			}
			else{
				System.out.println("");
				System.out.println("That was an invalid selection. Please make another.");
				System.out.println("");
			}
				column = 0;
		    	row = 0;
		    	while (row<2){
		    		while (column<10){
		    		System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
		    				+myTheatre2D[row][column] +" dollars."
		    				+ "That seat is currently " + seatStatus[row][column]);
		    		column+=1;
		    		}
		    		row+=1;
		    		column = 0;
		    }
        }catch (NumberFormatException e){
			System.out.println("");
        	System.out.println("Sorry that is not a valid selection");
			System.out.println("");
        }
        }
   	}
}

My final problem is that I need to accept an input of null in order to quit the program.

You can add a final statement in the if else loop to quit the program if the transaction is undone.

It should be like,

else
{
exit(0);
}
that will fix the accepting a null value.
It automatically quits the program if the selection is undone.

I'm not sure I understand what you mean. I could place it witin the "else if" statement as you said but that would quit the program if they enetered a value that is not an integer. Do you mea to put it into the catch statement? Could you please tell me which line you are talking about?

If you are looking to quit the program after the user enters a specific value, then add an IF statement to check for that value. For example (from line 78)

String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
          int crownum = Integer.parseInt(crow);
          if (crownum == 0){
              return;
          }

The return statement will exit the app. You can add whatever you want in the if statement, like a goodbye message. Let me know how this works.

Alright the code is working wonderfully however I would like to seperate this into two classes. Here is the current code:

import javax.swing.JOptionPane; //imports the library required to use GUI
public class Theatre2D { //Start of class
	/**
	 * @param args
	 */
	public static void main(String[] args) { //Start of method
		// TODO Auto-generated method stub
        //**********************************************************
        int[][] myTheatre2D; // declares an array of integers
        myTheatre2D = new int[2][10];// allocates memory for 2*10 integers
        myTheatre2D[0][0] = 30; //sets a data value to the array position
        myTheatre2D[0][1] = 40; //sets a data value to the array position
        myTheatre2D[0][2] = 50; //sets a data value to the array position
        myTheatre2D[0][3] = 60; //sets a data value to the array position
        myTheatre2D[0][4] = 70; //sets a data value to the array position
        myTheatre2D[0][5] = 70; //sets a data value to the array position
        myTheatre2D[0][6] = 60; //sets a data value to the array position
        myTheatre2D[0][7] = 50; //sets a data value to the array position
        myTheatre2D[0][8] = 40; //sets a data value to the array position
        myTheatre2D[0][9] = 30; //sets a data value to the array position
        myTheatre2D[1][0] = 20; //sets a data value to the array position
        myTheatre2D[1][1] = 30; //sets a data value to the array position
        myTheatre2D[1][2] = 40; //sets a data value to the array position
        myTheatre2D[1][3] = 50; //sets a data value to the array position
        myTheatre2D[1][4] = 60; //sets a data value to the array position
        myTheatre2D[1][5] = 60; //sets a data value to the array position
        myTheatre2D[1][6] = 50; //sets a data value to the array position
        myTheatre2D[1][7] = 40; //sets a data value to the array position
        myTheatre2D[1][8] = 30; //sets a data value to the array position
        myTheatre2D[1][9] = 20; //sets a data value to the array position
        //creates the array of integers and allocates the memory for 2*10 integers
        String[][] seatStatus = new String[2][10];
        seatStatus[0][0] = "Open"; //sets the initial data value to the array position
        seatStatus[0][1] = "Open"; //sets the initial data value to the array position
        seatStatus[0][2] = "Open"; //sets the initial data value to the array position
        seatStatus[0][3] = "Open"; //sets the initial data value to the array position
        seatStatus[0][4] = "Open"; //sets the initial data value to the array position
        seatStatus[0][5] = "Open"; //sets the initial data value to the array position
        seatStatus[0][6] = "Open"; //sets the initial data value to the array position
        seatStatus[0][7] = "Open"; //sets the initial data value to the array position
        seatStatus[0][8] = "Open"; //sets the initial data value to the array position
        seatStatus[0][9] = "Open"; //sets the initial data value to the array position
        seatStatus[1][0] = "Open"; //sets the initial data value to the array position
        seatStatus[1][1] = "Open"; //sets the initial data value to the array position
        seatStatus[1][2] = "Open"; //sets the initial data value to the array position
        seatStatus[1][3] = "Open"; //sets the initial data value to the array position
        seatStatus[1][4] = "Open"; //sets the initial data value to the array position
        seatStatus[1][5] = "Open"; //sets the initial data value to the array position
        seatStatus[1][6] = "Open"; //sets the initial data value to the array position
        seatStatus[1][7] = "Open"; //sets the initial data value to the array position
        seatStatus[1][8] = "Open"; //sets the initial data value to the array position
        seatStatus[1][9] = "Open"; //sets the initial data value to the array position
        //**********************************************************
    	int column = 0; //creates the column counter and sets it to a value of 0
    	int row = 0; //creates the row counter and sets it to a value of 0
    	while (row<2){ //loops until it reaches the third row(which non-existent) so it quits
    		while (column<10){ //loops until it reaches the 11th column (which non-existent) so it quits
    			//Prints output to the user allowing the user to see which seats are available and the cost of each
    			System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
    				+myTheatre2D[row][column] +" dollars."
    				+ "That seat is currently " + seatStatus[row][column]);
    		column+=1; //Adds to the column counter
    		} //end the while column loop
    		row+=1; //adds to the row counter
    		column = 0; //resets column to a value of 0 to restart the loop for the second row
    	} //end the while row loop
    	int bookcounter = 0; //creates the bookcounter variable to count the number of seats that have been booked
        while (bookcounter<20){ //loops while the number of seats booked is less than 20
        	try{ //trys to prompt the user for input
        	String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) <0 to quit>");
			int crownum = Integer.parseInt(crow); //changes input to an integer
			if (crownum==0){//breaks if 0 is inputed
				System.out.println("Goodbye!"); //prints output
				break;//breaks the loop
			}else{//continues if a different number is inputed
			String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10) <0 to quit>");
        	int cseatnum = Integer.parseInt(cseat);//changes the input into an integer
				if(cseatnum==0){//breaks if 0 is inputed
					System.out.println("Goodbye!"); //prints output
					break;//breaks the loop
				}else{//continues with the operations if the user does not wish to quit
			if (seatStatus[crownum-1][cseatnum-1].equals("Open")){//if the seat is open
				seatStatus[crownum-1][cseatnum-1] = "Occupied";//changes the value to occupied
				System.out.println(""); //prints a blank line
				System.out.println("Thanks for you purchase! It has cost you $"+myTheatre2D[crownum-1][cseatnum-1]+".");//provides feedback
				System.out.println(""); //prints a blank line
				bookcounter+=1;//adds to the number of seats booked
			}//end if statement
			else if(seatStatus[crownum-1][cseatnum-1].equals("Occupied")){//if the seat has already been booked
				System.out.println(""); //prints a blank line
				System.out.println("Unfortunately that seat has been taken. Please chose another seat.");//provides feedback		
				System.out.println(""); //prints a blank line
			}//end else
			else{ //if the user inputs a non integer
				System.out.println(""); //prints a blank line
				System.out.println("That was an invalid selection. Please make another.");//provides feedback
				System.out.println(""); //prints a blank line
			}//end else
				column = 0; //sets column to a value of 0
		    	row = 0; //sets row to a value of 0
		    	while (row<2){ //loops until it reaches the third row(which non-existent) so it quits
		    		while (column<10){ //loops until it reaches the 11th column (which non-existent) so it quits
		    			//Prints output to the user allowing the user to see which seats are available and the cost of each
		    			System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs " 
		    				+myTheatre2D[row][column] +" dollars."
		    				+ "That seat is currently " + seatStatus[row][column]);
		    		column+=1; //Adds to the column counter
		    		} //end the while column loop
		    		row+=1; //adds to the row counter
		    		column = 0; //resets column to a value of 0 to restart the loop for the second row
		    	} //end the while row loop
			}//end else from a non zero cseatnum
        	}//end else from a non zero crownum
        }catch (NumberFormatException e){//catches a non integer value and ends the try
			System.out.println(""); //prints a blank line
        	System.out.println("Sorry that is not a valid selection");//provides feedback
			System.out.println(""); //prints a blank line
        }//end catch
        }//end book counter while loop
   	}//end method
}//end class

I would like to isolate the section that I have surrounded by astrixes into its own class that would be called Initialize. I have no idea where to start and if somebody could give me any insight as to how to access array data between classes (read and write) that would help a lot.

It really doesn't make a lot of sense to try to split this into two classes like that, but two (or more) methods would certainly help make the code easier to understand.
Eg: You could create your two arrays in the main method, then pass them as parameters to an "initialise" method that fills them with values.

I am not 100% confident working with multiple classes and am completely lost in how I can assign values to the array in one class and manipulate the array withing the main. I have searched my textbook and various web sites but can't find any help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.