The moose likes Beginning Java and the fly likes Bank program class or switch problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bank program class or switch problem" Watch "Bank program class or switch problem" New topic
Author

Bank program class or switch problem

Bryce Heath
Greenhorn

Joined: Sep 16, 2011
Posts: 19
Hey guys
Im writing a banking program to practice a little of what I have learned so far. I think I am having a problem with the class I made "Account" the problem it most likely that I dont understand classes right lol. The specific problem I seem to have is in the driver, in my Switch statement. What I am looking to do is have the program ask for the user to input there name, then an initial deposit into the bank account. Then I want the worker class to create an account object "acct1" then print out the initial deposit and balance. Im not really sure where I messed up, sorry I know a very broad question, hope some one sees what im doing wrong .

Driver


Worker

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don't use double arithmetic for money. Use BigDecimal instead.

I shall be surprised if you get that class to compile. You are trying to assign something to the value of the Scanner, which isn't a Scanner. You call nextLine() (so far, all correct) but you never use its return value. So the line input disappears into cyber-limbo, taking the owner's name with it, never to be seen again. You need to assign that return value to something, which you will later pass to the Account constructor.
Then you ask for an initial balance, creating another Scanner (you only need one Scanner for System.in), and again don't use that value. You also have two identifiers called balance and acctName which appear mysteriously on line 36, which the compiler will complain about, too.

What I would suggest is a testing method which looks like this:Get that lot working, then think about scanners. You are making life difficult for yourself by trying to do too much all at once. You should write not more than 5 lines before compiling and running the code. And fred will tell me off because he says you should compile every 3rd line
Bryce Heath
Greenhorn

Joined: Sep 16, 2011
Posts: 19
Hi again
Thanks for the reply man, ok cool what you said makes sense. Really I just started throwing this together yesterday to see if I know how to use what I have read so far, so ty for sticking with my silly problems lol. Anyway ill start from the top, good to know on the scanner class I thought I might have to put the input into a variable but was not sure. In the case of the users name

the user enters a string and creates the name object, then puts the string entered into the owner variable that is part of the worker class making both owner and acctName the user entered string. Is that right? Im going to take your advice and go back and try to start from the top but if you get time please post back. I think I dont understand the relationship between the driver and the worker correctly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
No, that ain't the right way to use a Scanner.If you get problems with the nextLine() method, if it apparently returns an empty String, then you need to read the documentation for nextLine(). It doesn't quite do what you thought. There is another explanation of a similar problem here, complete with some spectacular spelling errors!
Bryce Heath
Greenhorn

Joined: Sep 16, 2011
Posts: 19
Ok cool man, thanks for the links in the book it tends to go over things in one paragraph, never really explaining how they work fully. Ill take a look at the links hopefully it will help because when I run this the first switch and scan work fine but when it gets into case 1 both system prints run but the program terminates without letting the user type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Well, your switch-case only takes the input; it doesn't do anything with it. You will have to request a starting balance, then you can create an Account object with it

And . . . you're welcome
Bryce Heath
Greenhorn

Joined: Sep 16, 2011
Posts: 19
Yeah lol ill cross that path when it comes, ive gone back to step 1 to work out all the bugs before I move on. The problem I am currently having is the Scanner is not working in my first case if I remove it from the case it works fine, just doesent take input while its inside the case. Any idea why?

prints
Please enter account name
Welcome:
end program

If removed from the switch
works fine IE
Please enter account name
Welcome: "account name"
end program
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bank program class or switch problem