I have highlighted my error in red, not sure what is going on, this program should allow the user to enter a choice and not exit until they enter a choice of 5 or higher. That is what the loop does and the user selection allows them to start an account, deposit, withdraw, or see their balance, or of course quit. Thought it would be nice to fill you in on whats going on, thanks for your help!
EDIT -- long code causing weird formatting and horizontal scroll in thread
Nick Harms wrote:I know what you guys are saying but my teacher has already approved the way i have it layed out, im just having error problems. Maybe it's because you dont understand my assingment.
You are not allowed to have code that is not in a constructor, initializer, or method -- and having your teacher approve of that isn't going to change that.
The syntax is wrong and the compiler is telling you that. Henry and I agree.
I'll spend all night on here helping you if you wanna put the work in and pay attention to what we're saying. I won't just fix it and post the solution here for you.
It's up to you. The alternative is to fight with incorrect syntax all night and hand it in tomorrow incomplete.
Janeice DelVecchio wrote:For an example:
the do while loop should be in the main method and the other method should be declared OUTSIDE the main method. Then you can call it when you need it.
Im confused, how can I declare a method outside the main method while the loop is in the main method and the other methods are inside it. what you posted is an exact copy of mine correct, not a fix, because if so im missing something, god im cofused is there a way a to declare the methods outside the loop and call them in? I have never seen that done and wouldn't know were to start. My instructor told me to write my methods and inclose them in the do while loop. Thanks you for your help and baring with me
Nick Harms wrote:Just to be clear i wasn't being rude, sorry I just was making sure you didnt need more info about it from me.
So.... to start out with.... LEAVE the do/while loop and cut the methods out and paste them OUTSIDE the curly braces of the main method and INSIDE the curly braces of the class.
Post what you have (watch for that long line) when that's done.
This is a method:
You need to extract each of these (I suggest you start with the original because I explained wrong) WITHOUT the do/while and switch/case business.... and pop them OUTSIDE the main method curly braces and inside the class curly braces.
Just the methods belong in a class.
Code (that means ifs, dos, switches, etc) belongs in a method.
Methods cannot live inside other methods.
Nick Harms wrote:
That's really, really difficult to read.
And, in fact, in this case, proper indentation would help you identify (or at least start to identify) where things are going crazy.
Right now you're trying to declare methods in the middle of a switch statement. That won't work. Period. No matter what your teacher says.
You can *call* methods from your switch statement, but the methods themselves still need to be declared using valid, legal Java syntax.
Your switch statement isn't even *in* a method (I'm pretty sure this was pointed out earlier). It needs to be. We're not just making up random syntax rules to mess with your head. And if your teacher told you to put the actual method declarations in essentially random places in the source... well, my first assumption would be that you've misunderstood.
Did you see Nick's latest post? We're working on getting method calls in the switch statement instead of entire methods
you should stay to help out
the expression evaluates like so:
1. the computer gets the value of balance to start out with
2. it sends that value to be processed in the method
3. balance is set to the return value of the method
so the answer, I guess, is it ALREADY pulls from the same balance, it just has that weird line to begin with.
Where does the startingBalance value come from?
I see you're setting it (potentially) twice. Does the user always input it? If so, why does the user go through the same interface AFTER that to start a new account?
Nick Harms wrote:If you can provide email i can email you my assingment, it is a word document.
The purpose of this assingment is too know how to succefully call in methods. I have already argued this is not practical, and she says that is not the purpose.
Now i have switched starting balance to balance in the call method area do i need to switch the doubles below for it to call correctly?
here is the instructions
Please write the following program using methods. All methods will be called from the main method.
Write a program titled: BankAccount. The purpose of the program is to allow the user to open a BankAccount with a starting deposit as the starting balance. The program will have methods that will allow the user to make a deposit or withdrawal. Please follow the outline below:
The main method will display a message to the user stating what the purpose of the program is.
The Main method will first call the Beginning balance method to determine the beginning balance and return the balance.
The main method will ask the user what type of transaction they are making (hint: Use a do while loop to display a menu and call each method based upon the choice given by the user). The methods are described below. After each method is called, the method will return the new balance and the main method will display the new balance.
Beginning balance – Ask the user for the amount they will deposit to open the account which will be returned as the starting balance;
Deposit – When called the deposit method will receive the starting balance as a parameter and add the deposit, returning the new balance.
Withdrawal - When called the Withdrawal method will receive the starting balance as a parameter and subtract the withdrawal, returning the new balance.
Display Balance – This will be the last method called and will display the balance.
Don’t forget to pass parameters for each method to perform the task!
And you shouldn't wait til the day before to do your homework
This is now a question of the lifecycle of references/variables.
If you pass something into a method
bar gets named (locally, inside the method) whatever the name is here:
Once it leaves, the data in this method is gone. dead. Unless 2 things happen (on a very basic level... there are exceptions).
1. the method returns something
2. you catch that return value somehow by making something equal to it
so the "startingBalance" in the parenthesis of the methods you made, is just the starting balance before the method runs. Get it?
The compiler is warning you that double is bigger than an int and it might not fit into the smaller datatype.
Declare it properly (and make sure the scanner will read it right...) and it'll be good.