Please help with this THEATER programa

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2876 Views - Last Post: 22 March 2009 - 12:43 PM Rate Topic: -----

#1 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Please help with this THEATER programa

Post icon  Posted 19 March 2009 - 02:45 PM

Everything is working fine, but when we select number 4, for purchase a ticket the program is crashing. Please anybody can help with this:

import java.util.*;

public class Theater {

	 public static void main(String[] args) {

		char[][] seats = new  char [15][30];
		double[] prices = new double [15];

		Scanner key = new Scanner(System.in);	  

		for (int i=1; i<seats.length; i++) {
			for (int j=1; j<seats[0].length; j++) {
			seats[i][j]='#';

		  }
	}

		 for (int i=1; i<=15; i++) {
			 System.out.println("Please enter price for Row: #"+ i);
					prices[i-1]=key.nextDouble();
				   
					 if (prices[i-1]<0) {
		  System.out.println("Price should be a positive number"); 
		  i--;
  
		}  
	  
		   }
	   

   displayMenu();


 int seleccion=getChoice(key);
 

while (seleccion!=5) {



  if (seleccion==1) {
		  System.out.println("Asientos disponibles # , Asientos ocupados *");
		  for (int i=0; i<seats.length; i++) {
		   for (int j=0; j<seats[0].length; j++) {

			System.out.print(seats[i][j]+" ");

			 }
		   System.out.println();
		   
	   }
		  
}

  else  if (seleccion==2) {

			displayPrices(prices);

		}
  else  if (seleccion==3) {

	   double totalsales = 0;
	   System.out.println("Ticket sales:");

   }

  else if (seleccion==4) {

  purchaseTicket(seats[][]);
 }
 else {
		   System.out.println("Invalid selection");
 }

	displayMenu(); //call the menu

	seleccion=getChoice(key);
}
	 }
	
 
		public static int getChoice(Scanner key) {
		
		int numsel=key.nextInt();

		if (numsel<0 || numsel>5) {
			System.out.println("The selection should be between 1-5");
		   displayMenu();
		   return getChoice(key);
		}

		 return numsel;

		}


		public static void displayMenu() {
		System.out.println("THEATER SOFTWARE MENU");
		System.out.println("1. View Available Seats");
		System.out.println("2. View Seating Prices");
		System.out.println("3. View Ticket Sales ");
		System.out.println("4. Purchase a Ticket");
		System.out.println("5. Exit the Program");
		System.out.print("ENTER YOUR CHOICE: ");

		}

		public static void displayPrices(final double prices[]) {

			System.out.println("Prices per row:");

			for (int i=1; i<=15; i++)
			{
				System.out.print("Row: #"+i+": ");
				System.out.println(prices[i-1]);

			}
		}
public static void purchaseTicket(char seats[][], final double prices[]){


System.out.println("Please enter row where you want to seat");
Scanner key = new Scanner(System.in);
int rownum=key.nextInt();
   int col=key.nextInt();

   if (seats[rownum][col]=='#') {
	   seats[rownum][col]='*';
		double totalsales=0;
	  totalsales=totalsales+prices[rownum];
		System.out.println("You have been purchase seat"+rownum+col);

   }

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Please help with this THEATER programa

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 02:48 PM

What is crashing about your program. What is the error?
Was This Post Helpful? 0
  • +
  • -

#3 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 02:51 PM

View Postmarkhazlett9, on 19 Mar, 2009 - 01:48 PM, said:

What is crashing about your program. What is the error?

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at Theater.main(Theater.java:80)
Java Result: 1
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 03:03 PM

Ok your method puchase ticket is meant to accept 2 arrays... one 2-dimensional and 1 single... when you call the method inside your switch statement at case 4, you are only submitting a 2-dimensional array and not the second one. When you call a method it has to accept all the elements necessary.
Was This Post Helpful? 0
  • +
  • -

#5 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 03:55 PM

ok thanks,
Now we have this problem: I'm trying to to ask the user if they want to see aviable seats before buy, but we are having troobles.

This is my code now:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Manuel Santamaria
 */

import java.util.*;

public class Theater {

	 public static void main(String[] args) {

		char[][] seats = new  char [15][30];
		double[] prices = new double [16];
		double totalsales=0;


		Scanner key = new Scanner(System.in);	  

		for (int i=1; i<seats.length; i++) {
			for (int j=1; j<seats[0].length; j++) {
			seats[i][j]='#';

		  }
	}

		 for (int i=1; i<=15; i++) {
			 System.out.println("Please enter price for Row: #"+ i);
					prices[i]=key.nextDouble();
				   
					 if (prices[i]<0) {
		  System.out.println("Price should be a positive number"); 
		  i--;
  
		}  
	  
		   }
	   

   displayMenu();


 int seleccion=getChoice(key);
 

while (seleccion!=5) {



  if (seleccion==1) {
		  System.out.println("Asientos disponibles # , Asientos ocupados *");
		  for (int i=0; i<seats.length; i++) {
		   for (int j=0; j<seats[0].length; j++) {

			System.out.print(seats[i][j]+" ");

			 }
		   System.out.println();
		   
	   }
		  
}

  else  if (seleccion==2) {

			displayPrices(prices);

		}
  else  if (seleccion==3) {

	   
	   System.out.println("Ticket sales:"+totalsales);

   }

  else if (seleccion==4) {

  purchaseTicket(seats, prices,totalsales);
 }
 else {
		   System.out.println("Invalid selection");
 }

	displayMenu(); //call the menu

	seleccion=getChoice(key);
}
	 }
	
 
		public static int getChoice(Scanner key) {
		
		int numsel=key.nextInt();

		if (numsel<0 || numsel>5) {
			System.out.println("The selection should be between 1-5");
		   displayMenu();
		   return getChoice(key);
		}

		 return numsel;

		}


		public static void displayMenu() {
		System.out.println("THEATER SOFTWARE MENU");
		System.out.println("1. View Available Seats");
		System.out.println("2. View Seating Prices");
		System.out.println("3. View Ticket Sales ");
		System.out.println("4. Purchase a Ticket");
		System.out.println("5. Exit the Program");
		System.out.print("ENTER YOUR CHOICE: ");

		}

		public static void displayPrices(final double prices[]) {

			System.out.println("Prices per row:");

			for (int i=1; i<=15; i++)
			{
				System.out.print("Row: #"+i+": ");
				System.out.println("Price:$"+prices[i]);

			}
		}
public static void purchaseTicket(char seats[][],double prices[],double totalsales){

	   System.out.println("Do you wish to view chart of available seats?");

	  String question;
	  question=key.nextLine();

		  if (question.charAt(0)=='Y' || question.charAt(0)=='y'){

}
	   
System.out.println("Please enter row where you want to seat");
Scanner key = new Scanner(System.in);
int rownum=key.nextInt();
if (rownum>15 || rownum>1){
System.out.println("Row need to be between 1-15");
displayMenu();
}
System.out.println("Please enter col number you like to seat");
   int col=key.nextInt();
   if (seats[rownum][col]=='#') {
	   seats[rownum][col]='*';
	  totalsales=totalsales+prices[rownum];
		System.out.println("You have been purchase seat "+rownum+","+col);

   }
   else if (seats[rownum][col]=='*') {

	   System.out.println("This seat is already sold");
	   
   }

}
}
	 
	  
   


  

Was This Post Helpful? 0
  • +
  • -

#6 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 04:08 PM

I'm a little confused.. I don't see any method in relation to the view available seats?!? Please supply a few more details about your issue.
Was This Post Helpful? 0
  • +
  • -

#7 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 04:19 PM

No, tghere is not a method,
is the selecion 1

if (seleccion==1) {
		  System.out.println("Asientos disponibles # , Asientos ocupados *");
		  for (int i=0; i<seats.length; i++) {
		   for (int j=0; j<seats[0].length; j++) {

			System.out.print(seats[i][j]+" ");

			 }
		   System.out.println();
		   
	   }
		  
}




We add this to the method;
public static void purchaseTicket(char seats[][],double prices[],double totalsales){

	   System.out.println("Do you wish to view chart of available seats?");

	  String question;
	  question=key.nextLine();

		  if (question.charAt(0)=='Y' || question.charAt(0)=='y'){

}
	   



So if the user press Y or y, it should show the users the chart of aviable/unavelable seats.
But we are having problem , maybe declaring the string inside the method.
Was This Post Helpful? 0
  • +
  • -

#8 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 04:24 PM

The if statement has no code inside of it. So even if the user presses "y" or "Y" It will not do anything. Or is it failing before it gets to that point?
Was This Post Helpful? 0
  • +
  • -

#9 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 04:29 PM

it its faileing before..
We will put as statement that..
But it is failing before that.
If you run the program and you type number 4 on the main menu you will see
Was This Post Helpful? 0
  • +
  • -

#10 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 05:03 PM

?????
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Please help with this THEATER programa

Posted 19 March 2009 - 06:16 PM

for (int i=1; i<=15; i++) {
			 System.out.println("Please enter price for Row: #"+ i);
					prices[i]=key.nextDouble();
				   
					 if (prices[i]<0) {
		  System.out.println("Price should be a positive number"); 
		  i--;	 // <-----------------------------------------------------------------------


Basic programming practice: NEVER change the index of a for loop inside the for loop
Was This Post Helpful? 0
  • +
  • -

#12 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 20 March 2009 - 07:21 PM

I already have the program complety working, but i have a problem now. i am getting this erro:
Purchase confirmed, you have been purchase seat Row:1,Seat:24
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

Anyone know why i am gettins this error?
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Please help with this THEATER programa

Posted 20 March 2009 - 07:23 PM

Post the code.

I'll use pbl's idiom...

Trying to make us fix a problem without posting the code is like telling your mechanic to fix your engine without looking under the hood. ;)

This post has been edited by Locke: 20 March 2009 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

#14 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 20 March 2009 - 09:09 PM

This is the erro rafter buyt a ticket:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at Theater.purchaseTicket(Theater.java:172)
at Theater.main(Theater.java:84)
Java Result: 1


This is the code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Manuel Santamaria
 */

import java.util.*;

public class Theater {
	 static double[] totalsales= new double [1];
	 static String question;
	 
	 public static void main(String[] args) {

		char[][] seats = new  char [16][31];
		double[] prices = new double [16];
		


		Scanner key = new Scanner(System.in);	  

		for (int i=1; i<seats.length; i++) {
			for (int j=1; j<seats[0].length; j++) {
			seats[i][j]='#';

		  }
	}

		 for (int i=1; i<=15; i++) {
			 System.out.println("Please enter price for Row: #"+ i);
					prices[i]=key.nextDouble();
				   
					 if (prices[i]<0) {
		  System.out.println("Price should be a positive number"); 
		  i--;
  
		}  
	  
		   }
	   

   displayMenu();


 int seleccion=getChoice(key);
 

while (seleccion!=5) {



  if (seleccion==1) {
		  System.out.println("Seats available # , Seats Taken *");
		  for (int i=0; i<seats.length; i++) {
		   for (int j=0; j<seats[0].length; j++) {

			System.out.print(seats[i][j]+" ");

			 }
		   System.out.println();
		   
	   }
		  
}

  else  if (seleccion==2) {

			displayPrices(prices);

		}
  else  if (seleccion==3) {

	   
	   System.out.println("Ticket sales:"+totalsales[0]);

   }

  else if (seleccion==4) {

  purchaseTicket(seats, prices,totalsales);
 }
 else {
		   System.out.println("Invalid selection");
 }

	displayMenu(); //call the menu

	seleccion=getChoice(key);
}
	 }
	
 
		public static int getChoice(Scanner key) {
		
		int numsel=key.nextInt();

		if (numsel<0 || numsel>5) {
			System.out.println("The selection should be between 1-5");
		   displayMenu();
		   return getChoice(key);
		}

		 return numsel;

		}


		public static void displayMenu() {
		System.out.println("THEATER SOFTWARE MENU");
		System.out.println("1. View Available Seats");
		System.out.println("2. View Seating Prices");
		System.out.println("3. View Ticket Sales ");
		System.out.println("4. Purchase a Ticket");
		System.out.println("5. Exit the Program");
		System.out.print("ENTER YOUR CHOICE: ");

		}

		public static void displayPrices(final double prices[]) {

			System.out.println("Prices per row:");

			for (int i=1; i<=15; i++)
			{
				System.out.print("Row: #"+i+": ");
				System.out.println("Price:$"+prices[i]);

			}
		}
public static void purchaseTicket(char seats[][], final double prices[], double totalsales[]){

	  
	System.out.println("Do you wish to view chart of available seats?");

	Scanner key = new Scanner(System.in);
 
	  question=key.nextLine();

		  if (question.charAt(0)=='Y' || question.charAt(0)=='y'){

		 System.out.println("Seats available # , Seats Taken *");

		 for (int i=0; i<seats.length; i++) {
		   for (int j=0; j<seats[0].length; j++) {

			System.out.print(seats[i][j]+" ");

			 }
		   System.out.println();
}
		  }
	 
System.out.println("Please enter row where you want to seat(1-15)");

int rownum=key.nextInt();


System.out.println("Please enter seat number you like to seat(1-30)");
   int seat=key.nextInt();
   
   if (seats[rownum][seat]=='#') {
	  seats[rownum][seat]='*';
	  totalsales[0]+=prices[rownum];
	  System.out.println("Purchase confirmed, you have been purchase seat Row:"+rownum+",Seat:"+seat);
	  System.out.println("Do you want to purchase another ticket?");
	  
	  question=key.nextLine();
	   if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
		purchaseTicket(seats, prices, totalsales);
	   }

   }
   else if (seats[rownum][seat]=='*') {

	   System.out.println("This seat is already sold");
	   System.out.println("Do you want to purchase another ticket?");
	   question=key.nextLine();
	   if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
		purchaseTicket(seats, prices, totalsales);
	   }
   }

}
 
}

	 
	  
   


  

Was This Post Helpful? 0
  • +
  • -

#15 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 05-December 08

Re: Please help with this THEATER programa

Posted 20 March 2009 - 10:49 PM

?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2