aspose file tools*
The moose likes Beginning Java and the fly likes Having troubles with a complex program, need help with an illegal start of type error. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having troubles with a complex program, need help with an illegal start of type error." Watch "Having troubles with a complex program, need help with an illegal start of type error." New topic
Author

Having troubles with a complex program, need help with an illegal start of type error.

Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44




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
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
do { that was supposed to show the start of the do while loop in red

My program says do { is an illegal start of a type.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Code (that is not a declaration) has to be in a constructor, initializer, or method. You can't just place code anywhere. You do loop, along with many switch cases, fail in that regard.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Could be a little more descriptive? Im sorry but I have a program with a switch and cases similar to this and it works, I've been hassling with this all day, I would really appreciate a little more insight. Im waiting for the aw ha! moment lol
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Henry is right.... you need to learn basic syntax of classes and methods



When you do things right, people won't be sure you've done anything at all.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
I removed the } above the do so that the loop is included in the main method, but now i get an illegal start of expression in the first method of the loop
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Everything needs to be either a class/method signiture or surrounded by curly braces.

You'll have to fix all your algorithms to be inside a method.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
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.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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.

Henry
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

It's not that either Henry or I don't understand the assignment. I promise.

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.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
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
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Just to be clear i wasn't being rude, sorry I just was making sure you didnt need more info about it from me.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

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.


It's okay


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.

TRUST ME
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I'm sorry if I was unclear
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
Ranch Hand

Joined: Mar 06, 2010
Posts: 44

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

better.

So now, inside each of those case statements you need to perform a method call.

in the first one we're setting the balance to be the return value of the method that sets the balance.... right?

so:



see if you can figure out the others.....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

David...

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
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44


Im not sure if the last one is right, Hopefully you approve of my work
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
How do i get all of the cases to pull the same balance ? can the methods be named the same like use balanceaftertransaction for both 2 and 3? Thats one thing confusing me also.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
if so in the main method how would you write that? balance = balance after deposit or balance after withdrawal
Thats not my code just how im thinking.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Pretty good....

Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
But im pulling in my starting balance to add or subract from it
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

show me where.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Can i make a thing in the main method balance = starting balance and use that instead of starting balance were you are saying too
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Well what are you going to add or subract from if you dont pull in the starting balance from the user, in case 2 and 3.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I still don't see where you think you're using startingBalance.

Just paste a bit of code.... like 5 or so lines... where startingBalance is still used in the main method.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Oh, the first method returns balance, not starting balance
So method's 2 and 3 will pull in balance? and return it as balance?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

nevermind. I see it.... so.... why do you have the user enter a starting balance and then start a new account?

It would make more sense to scrap that line?
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
If you can provide email i can email you my assingment, it is a word document.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

once you do this:



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.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

if we don't discuss all our questions and answers here on the forum, how will everyone benefit?

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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nick Harms wrote:If you can provide email i can email you my assingment, it is a word document.

Please UseTheForumNotEmail.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44


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:
Main Method:
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.
Methods:
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!


Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
given the instructions I would say switching balance back to starting balance is in order, what say you?
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
Also

It says
switch(userChoice)
possible loss of precsion!
<-----haha jk I need a bit of humor at this point
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

You should buy HeadFirst Java. It's like $30 on Amazon and worth every penny. Not kidding.

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?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

userchoice should be declared as an int. Switches take ints as parameters (or enums, but that's a long story).

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having troubles with a complex program, need help with an illegal start of type error.