You are on page 1of 5

6/13/13

A simple boarding pass.... (Beginning Java forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Beginning Java

Author

A simple boarding pass....


posted 2/17/2005 4:15 AM

David J. Gonzo Greenhorn Joined: Feb 15, 2005 Posts: 20

Hello again.. I am attempting to design a simple console program that does the following. A small airline has just purchased a computer for its new automated reservations system. The president has asked you to program the new system in Java. You are to write a program to assign seats on each flight of the airlines only plane (capacity: 10 seats). Your program should display the following menu of alternatives: Please type 1 for smoking Please type 2 for nonsmoking If the person types 1, then your program should assign a seat in the smoking section (seats 1-5). If the person types 2, then your program should assign a seat in the nonsmoking section (seats 6-10). Your program should then print a boarding pass indication the persons seat number and whether it is in smoking or nonsmoking section of the plane. Use a single subscript array to represent the seating char of the plane. Initialize all the elements of the array to 0 to indicate all seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat is no longer available.Your program should, of course, never assign a seat that has already been assigned. When the smoking section is full, your program should ask the person if it is acceptable to be placed in the nonsmoking section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message Next flight leaves in 3 hours

This is what I have so far. It compiles but hangs. I could use some advice as to wether it is possible to get it to work this way or should I go in a completely different direction.

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 .

i m p o r tj a v a . i o . * ; i m p o r tj a v a . u t i l . * ; i m p o r tj a v a . l a n g . * ;

p u b l i cc l a s sb o a r d i n g P a s s { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g u m e n t s ){ S t r i n g [ ]b o o k i n g=n e wS t r i n g [ 9 ] ; i n tu s e r I n p u t ; i n ty e s N o ; i n ti=0 ; S y s t e m . o u t . p r i n t l n ( " P l e a s et y p e1f o r' s m o k i n g 's e c t i o no r2 s m o k i n g 's e c t i o n .P r e s s' e n t e r 'w h e nd o n e . " ) ; f o r' n o n -

t r y { / / t r yo p e n u s e r I n p u t=S y s t e m . i n . r e a d ( ) ; / / ab i to fe r r o rc h e c k i n g ,b u tIn e e dal i t t l em o r e . . . w h i l e (i< =1 0 ) { / / f o ro p e n

www.coderanch.com/t/398593/java/java/simple-boarding-pass

1/5

6/13/13
2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . e nd o n e . " ) ; t r y {

A simple boarding pass.... (Beginning Java forum at JavaRanch)


i f ( u s e r I n p u t= =1 ) { / / i fo p e n f o r ( i n ts=0 ;s< =5 ;s + + ) { / / f o ro p e n S y s t e m . o u t . p r i n t l n ( " B o a r d i n gP a s s :S m o k i n gS e a t : "+s ) ; S y s t e m . o u t . p r i n t l n ( " W o u l dy o ul i k et ob o o ka n o t h e rs e a t ?[ y / n ] .P r e s s' e n t e r 'w h y e s N o=S y s t e m . i n . r e a d ( ) ; i f ( y e s N o= =' y ' ) { s + + ; i + + ; S y s t e m . o u t . p r i n t l n ( " B o a r d i n gP a s s :S m o k i n gS e a t : "+s ) ; } e l s e { / / S y s t e m . e x i t ( 0 ) ; i + + ; } } c a t c h ( E x c e p t i o ne ) { S y s t e m . o u t . p r i n t l n ( " E R R O R " ) ; } } / / f o rc l o s e } / / i fc l o s e e l s e { / / e l s eo p e n f o r ( i n tn=5 ;n< =1 0 ;n + + ) { / / f o ro p e n S y s t e m . o u t . p r i n t l n ( " B o a r d i n gP a s s :N o n S m o k i n gS e a t : "+n ) ; S y s t e m . o u t . p r i n t l n ( " W o u l dy o ul i k et ob o o ka n o t h e rs e a r ?[ y / n ] .P r e s s' e n t e r 'w h e nd o n e . " ) ; y e s N o=S y s t e m . i n . r e a d ( ) ; i f ( y e s N o= =' y ' ) r e t u r n ; e l s e S y s t e m . e x i t ( 0 ) ; i + + ; } / / f o rc l o s e } / / e l s ec l o s e S y s t e m . o u t . p r i n t l n ( " N e x tf l i g h tl e a v e si n3h o u r s " ) ; } / / f o rc l o s e } c a t c h ( E x c e p t i o ne ) { S y s t e m . o u t . p r i n t l n ( " e r r o r " ) ; } f i n a l l y { S y s t e m . o u t . p r i n t l n ( " T h a n kY o u " ) ; }

marc weber Sheriff Joined: Aug 31, 2004 Posts: 11343


I like...

posted 2/17/2005 5:05 AM

Originally posted by David J. Gonzo: ... It compiles but hangs...

Actually, the first problem is that it doesn't hang. After it assigns the first seat then asks whether the user wants to book another, it immediately ends without waiting for a response. I suggest you look at some alternatives to using System.in.read(), which reads one byte at a time. Are you allowed to use new Java 1.5 features? On a more general level, I suggest that you consider this as an object (with its own fields and methods) rather than one big process stuffed into main. Break it down by writing and testing one method at a time. [ February 16, 2005: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org Merrill Higginson Ranch Hand Joined: Feb 15, 2005 Posts: 4864

posted 2/17/2005 5:19 AM

Try the following method or something similar for reading input from the console: String getUserInput(){ InputStreamReader input = new InputStreamReader(System.in); BufferedReader stream = new BufferedReader( input ); String answer = new String(); try{ answer = stream.readLine(); } catch (java.io.IOException e){ return answer;

www.coderanch.com/t/398593/java/java/simple-boarding-pass

2/5

6/13/13
return answer; } return answer; } Merrill

A simple boarding pass.... (Beginning Java forum at JavaRanch)

Merrill C onsultant, Sima Solutions

David J. Gonzo Greenhorn Joined: Feb 15, 2005 Posts: 20

posted 2/17/2005 5:27 AM

Thank You so much... I will use the advice. This should keep me busy for a while...

David J. Gonzo Greenhorn Joined: Feb 15, 2005 Posts: 20

posted 2/17/2005 6:13 AM

O.K I think I am on my way. Could you take a look at the following and tell me what you think? I am getting a ';' error, and cannot seem to find it...

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 .

i m p o r tj a v a . i o . * ; i m p o r tj a v a . u t i l . * ; i m p o r tj a v a . l a n g . * ;

p u b l i cc l a s sb o a r d i n g P a s s S t e p { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g u m e n t s ){ S t r i n g [ ]b o o k i n g=n e wS t r i n g [ 9 ] ; S t r i n gg e t U s e r I n p u t ; S t r i n gg e t U s e r I n p u t ( ) { I n p u t S t r e a m R e a d e ri n p u t=n e wI n p u t S t r e a m R e a d e r ( S y s t e m . i n ) ; B u f f e r e d R e a d e rs t r e a m=n e wB u f f e r e d R e a d e r (i n p u t) ; S t r i n ga n s w e r=n e wS t r i n g ( ) ; t r y { a n s w e r=s t r e a m . r e a d L i n e ( ) ; } c a t c h( j a v a . i o . I O E x c e p t i o ne ) { r e t u r na n s w e r ; } r e t u r na n s w e r ; } v o i dS t a r t O u t ( ) { S y s t e m . o u t . p r i n t l n ( " P l e a s et y p e1f o r' s m o k i n g 's e c t i o no r2f o r' n o n s m o k i n g 's e c t i o n .P r e s s' e n t e r 'w h e nd o n e . " ) ; g e t U s e r I n p u t ( ) ; v o i dS m o k i n g ( ) { S y s t e m . o u t . p r i n t l n ( " B o a r d i n gP a s s :S m o k i n gS e a t : "+s ) ; S y s t e m . o u t . p r i n t l n ( " W o u l dy o ul i k et ob o o ka n o t h e rs e a t ?[ y / n ] .P r e s s' e n t e r 'w h e nd o n e . " ) ; } v o i dN o n S m o k i n g ( ) { S y s t e m . o u t . p r i n t l n ( " B o a r d i n gP a s s :N o n S m o k i n gS e a t : "+s ) ; S y s t e m . o u t . p r i n t l n ( " W o u l dy o ul i k et ob o o ka n o t h e rs e a t ?[ y / n ] .P r e s s' e n t e r 'w h e nd o n e . " ) ; } f o r ( i=0 ;i< =1 0 ;i + + ) { S t a r t O u t ( ) ; i f ( a n s w e r= =' 1 ' ) { f o r ( i n ts=0 ;s< =5 ;s + + ) { S m o k i n g ( ) ; g e t U s e r I n p u t ( ) ;/ / < = = = = = = = = = In e e ds o m e t h i n gh e r et h a tt a k e si tb a c ku pt ot h ep r i m a r y' f o r 's t a t e m e n t [ l i n e6 3 ] } } e l s e { f o r ( i n tn=5 ;n< =1 0 ;n + + ) { N o n S m o k i n g ( ) ; g e t U s e r I n p u t ( ) ; / / < = = = = = = = = = = = In e e ds o m e t h i n gh e r et h a tt a k e si tb a c ku pt ot h ep r i m a r y' f o r 's t a t e m e n t [ l i n e6 3 ] }

www.coderanch.com/t/398593/java/java/simple-boarding-pass

3/5

6/13/13
5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . } } }

A simple boarding pass.... (Beginning Java forum at JavaRanch)

S y s t e m . o u t . p r i n t l n ( " N e x tf l i g h tl e a v e si n3h o u r s " ) ;

} } }

marc weber Sheriff Joined: Aug 31, 2004 Posts: 11343


I like...

posted 2/17/2005 6:21 AM

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 .

b o a r d i n g P a s s S t e p . j a v a : 1 4 :' ; 'e x p e c t e d S t r i n gg e t U s e r I n p u t ( ) { ^

The compiler is telling you that on line 14 of your code, it's expecting a semicolon between "getUserInput" and the parentheses. In other words, it's expecting a field declaration here rather than a method. Why? Because you're already in a method (main), and you can't define methods within methods.
Merrill Higginson Ranch Hand Joined: Feb 15, 2005 Posts: 4864 posted 2/17/2005 6:41 AM

The main problem with this program is that you are trying to put methods inside other methods. The Smoking(), NonSmoiking() and getUserInput() must all be placed after the StartOut() method, not inside it as is currently the case. Also, get in the habit of following java naming conventions: The class name should start with an Upper-Case letter (BoardingPassStep) and all method names and attribute names should start with lower case letters: (smoking(), nonSmoking(), ect). You're going to have to un-tangle this and get the methods separated out before you can go any further with this. Here's a basic outline to get you started: import java.io.*; import java.util.*; import java.lang.*; public class BoardingPassStep { BufferedReader stream = new BufferedReader(new InputStreamReader(System.in)); String[] booking = new String[10]; public static void main(String[] arguments) { BoardingPassStep pass = new BoardingPassStep(); pass.StartOut(); } private void StartOut() { String answer; boolean done = false; while (!done) { System.out.println("Please type 1 for 'smoking' section or 2 for 'non-smoking' section. Press 'enter' when done."); answer = getUserInput(); if (Integer.parseInt(answer)==1) { smoking(); } else { nonSmoking(); } } } private void smoking() { } private void nonSmoking() { } private String getUserInput() { String answer = new String(); try {

www.coderanch.com/t/398593/java/java/simple-boarding-pass

4/5

6/13/13
answer = stream.readLine();

A simple boarding pass.... (Beginning Java forum at JavaRanch)


} catch (java.io.IOException e) { return answer; } return answer; } }

Merrill [ February 16, 2005: Message edited by: Merrill Higginson ] [ February 16, 2005: Message edited by: Merrill Higginson ]

marc weber Sheriff Joined: Aug 31, 2004 Posts: 11343


I like...

posted 2/17/2005 11:53 PM

Here's the start of one possible approach to breaking the process down and writing methods to perform specific tasks. Note how the handleCustomer() method calls the other methods as needed. I think you'll see how much easier this would be to develop (and maintain) than one huge process in a single method.
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 .

i n tg e t P r e f e r e n c e ( ){ / / a s k su s e rf o rs m o k i n gp r e f e r e n c e / / r e a d sr e s p o n s ea n dr e t u r n si ta si n t } i n ts m o k i n g A v a i l a b l e ( ){ / / c h e c k ss e a t i n ga r r a yf o rv a c a n ts m o k i n gp o s i t i o n / / r e t u r n si n d e xo fa v a i l a b l es e a t ,o r1i fn o n ea r ea v a i l a b l e / / O Rt h i sm i g h tj u s tr e t u r nab o o l e a n } i n tn o n S m o k i n g A v a i l a b l e ( ){ / / n o n s m o k i n gv e r s i o no fa b o v e / / O Ry o um i g h tw r i t ej u s to n em e t h o da n dp a s st h e / / p r e f e r e n c ea sa na r g :" i n ta v a i l a b l e ( i n ts m o k e P r e f ) " } v o i db o o k ( i n ts e a t N u m b e r ){ / / s e t st h i se l e m e n tt o" 1 "i ns e a t i n ga r r a y / / p r i n t sb o a r d i n gp a s s } v o i dh a n d l e C u s t o m e r ( ){ i n tp r e f=g e t P r e f e r e n c e ( ) ;/ / c a l l sa b o v em e t h o d i f ( p r e f= =1 ){ i n ts e a t=s m o k i n g A v a i l a b l e ( ) ;/ / c a l l sa b o v em e t h o d i f ( s e a t> =0 ) b o o k ( s e a t ) ;/ / c a l l sa b o v em e t h o d e l s e. . . } e l s e. . . }

[ February 17, 2005: Message edited by: marc weber ]

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: A simple boarding pass....

Similar Threads Assignment print invalid selction not working Alphabet Pyramid Array Problem....sort of Need some help with Java program Airline.java
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 04:16:38 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/398593/java/java/simple-boarding-pass

5/5

You might also like