Results 1 to 9 of 9
Thread: Close but help would be grateful
- 12-19-2008, 09:36 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Close but help would be grateful
Hey guys i am learning java at college and i have a task to do.. basically my task is to create a airline booking system, this consists of:
Please select from the following options:
1- Book a luxury seat.
2 Book an executive seat.
3- Display seats/availability
4- Exit
Now the code can book a luxury seat ( though i have only created one for the timebeing). When I enter in two types of information from two different customers, I press option 2 and it asks for name( I want the name to find the name that was entered before binded to that seat) It will throw a wobbley saying " NullpointerException:null" at the if (tks [ count] = 0; count < ticketTot; count ++. I think i know why this is happening because its trying to show a int but its a String and its that what I want to show a String, is there anyway of getting around this problem?
Reader34
Java Code:import java.util.Scanner; class Libary { static final int quit = 0; public static int getMenuOpt() { Scanner menuOpt = new Scanner (System.in); int option; System.out.println("Welcome to CCN jet"); System.out.println("Please select a option below."); System.out.println("------------------------------"); System.out.println("1.Book a luxury seat"); System.out.println("2. list bookings "); System.out.println(quit + ".Quit"); option = menuOpt.nextInt(); return option; } public static int addTicket (Ticket tks [], int ticketTot) { Scanner input = new Scanner (System.in); String name; String seatClass; System.out.println("You have chosen to luxury"); System.out.print ("Enter your name "); name = input.nextLine(); System.out.print("Please comfirm booking type , Enter Luxury"); seatClass = input.nextLine(); System.out.println("thank you your details are :"+ name + seatClass ); tks [ticketTot] = new Ticket( name, seatClass); ticketTot++; return ticketTot; } public static void listByName (Ticket tks [], int ticketTot) { Scanner input = new Scanner (System.in); String name; String seatClass; System.out.print("Enter your name"); name = input.next(); for ( int count = 0 ; count < ticketTot; count ++ ) { System.out.println( tks [count].getName ()); System.out.println(tks [count].getSeatClass ()); } } public static void main (String [] args) { Scanner input = new Scanner (System.in); Ticket tickets [] = new Ticket [5]; int ticketsInLibrary = 1; int option; int seatsInLibrary=1; option = getMenuOpt(); while (option != quit) { if (option == 1) ticketsInLibrary = addTicket (tickets, ticketsInLibrary ); if (option == 2 ) listByName ( tickets, seatsInLibrary); System.out.println(" Seat number " + seatsInLibrary++ ); option = getMenuOpt(); } } } import java.util.Scanner; class Ticket { private String name; private String seatClass; Ticket ( String n, String s) { name = n; seatClass = s; } String getName () { return name; } String getSeatClass() { return seatClass; } }
- 12-20-2008, 03:11 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you bit clear it up?
Do you want to convert a string into an int to fix this problem? How can NullPointerException solve by that? It's better if you can copy-paste here full error message and the inputs you used.
- 12-20-2008, 03:20 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
Array index start from 0, not 1....
how can you add ticket to index 1 and remain index 0 as null?
- 12-20-2008, 03:22 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems you are dig into that long code. ;)
- 12-20-2008, 03:31 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
Practice only
- 12-20-2008, 03:32 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-20-2008, 06:39 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Ahhh thanks i changed both my values in the array from = 1 to = 0 and now when i enter in a name of someone who has booked a ticket there name type and seat number comes up. I have one question if anybody doesn't mind helping? I want to show a user how many seats are left and taken, would i use some sort of loop to do this? like count every seat and then show whats left.
thanks
reader34
- 12-21-2008, 05:29 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I hope you have all details about seats on a collection, array, list or anything...
- 12-21-2008, 05:42 PM #9
Arrays?
You can use an array to keep track of the seats. Since you're using arrays in your code, I'll assume you know the basics and don't need a tutorial, but here's the Array API so you can see all the methods that can be used:
Array (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
[SOLVED] Close Exe
By smartsubroto in forum New To JavaReplies: 11Last Post: 10-14-2008, 08:04 AM -
Close Active Window (?)
By dcnorman07 in forum New To JavaReplies: 2Last Post: 10-03-2008, 05:55 PM -
close a frame..
By tajinvillage in forum New To JavaReplies: 5Last Post: 04-27-2008, 10:22 PM -
Frame close operation
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:39 AM -
How to close a JFrame
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks