aspose file tools*
The moose likes Beginning Java and the fly likes help with program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with program" Watch "help with program" New topic
Author

help with program

Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
Hello. First time poster here.

I am learning Java via the wonderful Absolute Java book by Walter Savitch.

I am trying to work through one of the programming exercises and I am having trouble figuring out why the program is working like it is.

Here is the code:



When I execute the code and enter, say, 7 seat assignments, it only picks up the 1st, 2nd, 4th, and 6th entries. I suspect that it has something to do with how the scanner class works (I know you have to do console.nextLine() in order to discard the /n) but I can't seem to be able to correct it.

Can anyone help?

Thanks,
Charles
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to the Ranch.

Did you really find that program in a book? Have you copied it correctly? The long list of if statements is very awkward programming.

Problem with loops is that you have to re-initialise them. You are doing this by writingBut before that you writewhich reads the next line and returns it . . . and you are not using it, so the first of each pair of lines vanishes into some sort of cyber-limbo.

See if that helps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
. . . And have you counted how many seats are filled? Do you have some sort of test for double booking?
Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
Well, much of the code is mine, so that explains the "awkward programming".

Here is the original code received:



So my goal was to get it to work according to the problem:

Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:

1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D

The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:

1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D

After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another choice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I shall be off to bed soon, but . . .

Do your programming in bits. Never go more than 5 minutes without compiling and running your app. In fact you are probably better off running it every time you make the slightest change.

Set up the "while" loop, and the first time you run it, use this as your entire "user code:"Keep running it and observing the output until you are happy you have got it running correctly.

Find out about the following methods of the String class: toUpperCase toLowerCase charAt. They will allow you to get at the number and letter separately. That is a simplification possible because your app only ever has one letter and one number. Once you have got those data, try printing row and column separately.

Lots of beginners' books give the impression that "char"s are characters. They aren't. They are numbers, but displayed according to the Unicode values of those numbers. You can do arithmetic with chars. Try it. Try adding and subtracting chars from each other, see what 'z' - 'a' + 1 comes out to. See if you can use such arithmetic to get your row number.

I hope that gives you something to get on with.
Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
Thanks for your help! I was able to solve the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Well done. Please post what you achieved. It will give ideas to others to improve their programming.

You do realise my cheat about subtracting char variables will only work in a small aeroplane; if you get to row 10 the whole system collapses. But you have already found the substring method of String; look through the API until you find a method which can convert the String "10" to the number 10. Hint: Look in the "wrapper" classes Byte Short Integer and Long. There are rather similar methods in the Boolean Float and Double classes.

Actually, I don't like the way the example in the book was presented. You aren't creating enough objects, and people here will tell you, Java is an object-oriented language. You ought not to have all that code in the main method. There are lots of books which do things that way, and even the Java Tutorial. It takes less space on paper to demonstrate a syntactic trick, but it ain't object oriented.

A real OO way to do it might beAn Embraer has about 28 seats.

Now you want to create a BookingAgent class and a Plane class. They will need methods. The code inside the methods is exactly the same as what you have already. You just need to move it about a bit.

See if you can work that out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with program