File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why doesn't this program work??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't this program work???

Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi there, i have writen the following program (see code); but when i try to compile it; it says at the last line that i have to put a bracket ([expected) and i did that over and over again but still it says the same thing; i am really loosing hope so please help genoursley , thank you
best regards
Smith McFalan
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
In your actionPerformed close each if with a }
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
thank you Steven , but sadley after i di that; it says on all of the g.drawString statments the following: cannot find symbol ; what does this mean?
sorry for this but i am learning step by step :roll:
thank you
[ May 03, 2005: Message edited by: Smith McFalan ]
m brown
Ranch Hand

Joined: Mar 01, 2005
Posts: 57
i think the error is reffering to the ";" colon character you have in your code...you might have a colon out of place somewhere
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi there again, i took every semicolon away and still have the same error ; is there anything wrong with my code ?
thank you
Smith
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Each of your if statements should look like this.
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi steven and thank you again for your replies; i did what you said and still the same error message: here is the codes again:

Thank you
Smith
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Post the error message you're getting.
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
here are the codes Steven; and i really appreciate your help :

[ May 03, 2005: Message edited by: Smith McFalan ]
Krit Christoforou
Ranch Hand

Joined: Mar 01, 2005
Posts: 46
Hello Smith.
I'm not really an expert on java but have you got another class called Balloon?
[ May 03, 2005: Message edited by: Krit Christoforou ]
m brown
Ranch Hand

Joined: Mar 01, 2005
Posts: 57
well i see a few things wrong with your code.

First, your array



is declared incorrectly.....what do you want the name to be? party or ballon? and your new should refer to a primitive type like a int, char, etc...not a new Ballon

it should look like this{if you want your array to be a int)




second there is also a problem with your drawstring....i think you have to declare the g object..ex: "Graphics g" somewhere in your code. ill look at it some more when i have some more time.
[ May 03, 2005: Message edited by: m brown ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
actually

is correct the way it is. You have a reference named party that 'points' to an array of Balloon objects. The problem is that you either have not created a Balloon class, or it is not visible from where you are using it (you need to import it).

For the g, I assume you saw that in some other applet and are copying it (not a bad thing, but you didn't pay attention as to where it came from). The g you saw in the other applets was probably in the 'repaint' method (It's been awhile since I did applets so I could be a little off there). In the other applets is was passed into the method, something like

You are trying to use it in a place where it doesn't exist (outside that repaint method). What you need to do in your actionPerformed method is something more like this


The messageString variable would be declared at the class level and then you would do the drawString in the repaint method.
Jkarthi keyan
Greenhorn

Joined: Apr 27, 2005
Posts: 3
hi smith,

what steven suggested is absolutely right and You have to pass the message String variable in the below method.

public void paint(Graphics g){
g.drawString(messageString);
}

The applet will display the String that correspond to the the button clicked.


I also found another error in your code when the pricelist button is pressed you probably get an error. Check it out.


Regards
Jkarthi
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi there again, and thank you guys for helping me ; i truley appreciate it ; if i understood you lot correctley; you wanted me to declare a messageString variables and use a repaint; here what i did and the new codes are below; followed by two error messages; so i guess i am making progress as before it was 8 errors ; and yeah the class Ballon was wrongly named due to left over work that i was woring on ;


AND THE ERRORS:
Trial.java:58: ';' expected
; for (int messageString = 0;messageString++) {;
^
Trial.java:68: illegal start of expression
}
^
2 errors

Process completed.
[ May 04, 2005: Message edited by: Smith McFalan ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

you seem to have some stray semi-colons in there... i'm not SURE they're causing problems, but they don't help...

this line, for example:


why is there a semi-colon after the opening bracket?

there's also a stray one just above this line, all by itself.

a few lines after that, you seem to have an open brace "{" where maybe it should be a closing one, just before your repaint call. or maybe it shouldn't be there at all... haven't examined the code too closely yet...

p.s. you might still have problems with your array, since i don't see a reference to a Phone class anywhere...
[ May 04, 2005: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi there, and thank you for help AGAIN , i did what you said and added a class to the program called Phone so that the arrays can work; when i compiled it; it has only 1 error ; so i think i am on the way of fixing it and only need just a last push from you guys ; belwo is the new code with the class and the only error message :


HERE is the only error:
Trial.java:57: illegal start of expression
for (int messageString = 0 , messageString;++ ; {
^
1 error

Process completed.

THANK YOU
SmITh
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
You've still got a couple problems. The current error message is referring to your for loop, which is currently this


it should follow this pattern



Once that is cleared up I think you'll find that you haven't declared messageString anywhere in the trial class or your Phone class. Also I wouldn't use that same name for you for loop variable. Looks like you also have an extra bracket after your for loop.

Hope this helps
[ May 04, 2005: Message edited by: Hentay Duke ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

to help clarify what Hentay is saying, declaring

int messageString = 0;

is confusing. if i were looking at your code, and saw a variable messageString, i'd think it was, well, a String. i don't think it would ever cross my mind it would be an int.

the idiom for something like this is to use the variable i - which i assume stands for index. it's commonly used, and everyone who reads your code knows how i is being used almost without thinking about it.

the idiom for a for-loop is



any of the three fields can be there or omitted, but you MUST have the two semi-colons.

you probably want to initialize your counter to 0, so you start at the beginning. you want to test it to make sure it's less than the size of your array, and then you want to increment it by 1.

you probably want something like


[ May 04, 2005: Message edited by: fred rosenberger ]
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi guys, now i understand what was going on with that error ; thank you ; and you are right hentay; i compile the program after the changes and it says cannot resolve symbols in many places ; so i think it is because i didn't identify messageString anywhere ; how do i identify it and where shall i identify it?(at the start of the program, middle, or it doesn't matter) ?
thank you
smith
[ May 04, 2005: Message edited by: Smith McFalan ]
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
I would declare it at the top of your class, not inside any methods, like so.


In your phone class you'll also have to do something similar. In addition you'll need to pass that messageString from the trial class to the phone class. I'd just add another argument to the constructor. Also the drawString method expects a couple ints passed to it. So I've added them.

Study what's going on here and make sure you understand everything, if not come back and ask question. Judging from your code and a couple questions you've asked I would also suggest to read up on variable scope a little bit.



Finally please tell me this wasn't a homework assignment and I just finished it for you!

Good luck.
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi guys, and thank you Hentay for your last reply , i made the changes you suggested but still though ; i end up with 1 error; i guess it is not that bad as it is one error and from the error message i think it is simple; probably i forgot to add something somehwere ; below is the new improved codes with the only 1 error message; and i thank you guys again for your help; so we can get to the end of this program

and the ERROR MESSAGE :
Trial.java:17: 'class' or 'interface' expected
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You need to learn how to read the error messages and fix the problems you are having.

Lets look at your message:

Trial.java:17: 'class' or 'interface' expected

This says it's looking for the keyword class or interface on line 17. So we go to line 17 and we find

public void init() {

hmmm. That looks just fine, so maybe the error is being triggered because of something just before this line.

P.S. once you fix that error there will be another one. Look at it, try to understand what it says. If you can't figure it out post back and we'll try to help you through it. I don't think we will be doing you any favors by posting more code, you really need to understand why you are getting these errors.
[ May 04, 2005: Message edited by: Steven Bell ]
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi steven, i know you lot are getting bored of me and my errors but i did really try to fix it before asking about this error as like what you said the line seems to be fine so i don't know what is wrong ; i don't know what i have to do to get that PROCESS COMPLETED with no error sign
anyway, if anyone got any suggestion about why doesn't this program work please post a reply and your help is really appreciated
thank you
smith
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Smith,

Steven is right, try to fix this one -- or try again... You really need a success. And this one is really easy. This is similar to how a mother tiger gives its cub an easy kill to get experience...

Henry
[ May 04, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
ok guys , i will try to fix it somewhow, but i hope it is just the final error and then the program works ; ok then; no sleep tonight till this thing is sorted ; but please guys; if i can't make the kill ; i would appreciate some muscle power from you lot
thank you and will keep you lot updated about how the prey(Java) went.
Smith
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I gave you a BIG hint in my last post as to the cause of this error.

P.S. It's not that we are getting bored, it's that we really want to help you learn rather than do the work for you. Keep at it, the best way to learn is to do.
[ May 04, 2005: Message edited by: Steven Bell ]
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi there, results ; but no need to get happy the program doesn't work ; for the previous error the was an closed bracket between the public void inti(){ and the previous line that has the class,etc.. so that is why the error was saying class or interface expected as the bracket was stopping any feed of the class to it ; i am getting good at this, right ?
anyway, to the second problem ; there is an error saying; ilial start of expression; below is the codes for that part only followed by the error ; and as before i will try to fix it but i thought i might as well tell you of my progress.
thanks
smith


AND THE ERROR MESSAGE:
Trial.java:59: illegal start of expression
for (int messageString = 0 ; messageString < 100; messageString++)}
[ May 04, 2005: Message edited by: Smith McFalan ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
That should be a fairly easy one also. Go through the class carefull and make sure all your opening braces are in the right place and that they have a correctly places closing brace.

You are also going to have a compile problem with this line:

private String messageString = new messageString (x, y);

See if you can get it to compile, keep us up to date and let us know if you get stuck, then we'll talk about some design problems you have there. Good Luck.
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
hi Steven, i fixed that problem with other problems too; anyway at the end; the result is not goog ; i compiled it and ended up with 7 errors ; below is the new code with the errors following them; I am kind of beggining to loose hope ; as it seems that this program doen't seem to be able to work; why i don't know; everything is there; i read books and hours on the internet trying to figure why it doesn't work, do you guys thing i should just start all over again ; sorry for this depressing mood/reply; but i am really depressed with how things are going with this program.
p.s. you were right Steven; that line caused many probelms; it is so funny how one time you think you are so near to what you have been working on for days; and then suddenly everything falls apart
thank you
Smith



AND THE ERRORS:
Trial.java:10: cannot find symbol
symbol : class Phone
location: class Trial
private Phone[] party = new Phone[10];
^
Trial.java:10: cannot find symbol
symbol : class Phone
location: class Trial
private Phone[] party = new Phone[10];
^
Trial.java:12: cannot find symbol
symbol : class messageString
location: class Trial
private String messageString = new messageString (x, y);
^
Trial.java:12: x is not public in java.awt.Component; cannot be accessed from outside package
private String messageString = new messageString (x, y);
^
Trial.java:12: y is not public in java.awt.Component; cannot be accessed from outside package
private String messageString = new messageString (x, y);
^
Trial.java:49: cannot find symbol
symbol : variable RD2D
location: class Trial
if (event.getSource() == this.RD2D) {
^
Trial.java:63: cannot find symbol
symbol : class Phone
location: class Trial
party[messageString] = new Phone (x, y);
^
7 errors

Process completed.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You still have at least one brace out of place. That will clear up all the errors except for the ones about this line:

private String messageString = new messageString (x, y);

That line is still very wrong.
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
you are right Steven; it solved most of the problems with only left 2 errors; bellow is the code concerning the line with the error; and you are also right; i guees after solving those errors; it will still say something about that line ( private String messageString = new messageString (x, y); ) ; i seriosuly don't have a clue about what is wrong with this line; as from what Hentay said; (if i understood him correctley); is that i had to identify the message string


THE ERROR MESSAGES:
Trial.java:49: illegal start of type
if (event.getSource() == this.WD40) {
^
Trial.java:79: <identifier> expected
}
^
2 errors

Process completed.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

You are right, there is nothing wrong with the line... Take a look at the previous lines... This one is also easy. Just don't give up.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

this may be a dumb question at this point, but... how did you arrive at the original program you typed in?

The reason i ask is that you seem to have a lot of errors that all appeared at once. This implies that you typed in all 60 or 70 lines before you ever tried to compile it. i'm not surprised you're getting so many errors.

Generally, it's considered wise to write as little as possible between compiles. one or two lines, if you can get away with it. that way, if an error shows up, you know exactly where the error is, and you narrow down your search before you even start.

That's just advice for next time.

It seems that many of your problems here have to do with improperly placing braces. your code you post has strange formatting - i don't know if it looks strange in your editor - sometimes the cut-n-paste and the software on the ranch messes up the formatting. But if you don't have good formatting, you're hurting yourself. i'm talking about lines like these:


this is just not a good style. you'll hear people argue about where the braces should go (i.e. which lines, etc.), but i think just about everbody here would agree what you have here needs to be fixed. something like this:


this way, it's easy to tell what is inside the brackets. the closing bracket for the 'if' aligns with the 'if' itself. everything inside the brackets is indented the same amount (i chose 3 spaces, but it doesn't matter if you are consistant). Basically, every time you have an open bracket, every following line is indented 3 more spaces, until you have a closing bracket.

following this technique, it's a LOT easier to see when you have a bracket problem...
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Fred is giving excellent advice here. Pay close attention to what he's saying.

You really need to start over and go a little bit at a time, and no copying code from somewhere else and trying to fix it. That's not working for you. Do one thing at a time. Start with the trial class and just make it a bare class and get it to compile and then add one button and get it to compile then add an if block and compile etc... It may seem like a long process but it will be quicker in the long run than what your doing now.

Here's a start for you.



I know it's no where close to your program but you get the String working and you can move on.

Judging from this entire thread it seems like you need to learn about;
1.) variable scope
2.) declaring and initializing different types variables
3.) code formatting, ie... proper spacing and indents, opening and closing braces etc..
4.) learning to read error messages

These are all pretty basic stuff. Don't get discouraged it gets easier. Right now it seems like (and I'm just guessing here) you're trying to jump ahead or go to fast. Get the basics down and then move on and just do a little at a time (like Fred suggested) and get it working before moving on.

Good Luck
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Because you're working on a applet this might be a good thing to read:

http://java.sun.com/docs/books/tutorial/getStarted/applet/index.html
Smith McFalan
Greenhorn

Joined: May 03, 2005
Posts: 15
THANK YOU GUYS; i have done the program with a diffrent way; i started all over again; and this is my last codes below; it works how i wanted it.
thank you everyone especially Steven,Hentay, and all the rest.
thank you
Smith

[ May 06, 2005: Message edited by: Smith McFalan ]
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Congrats Smith
It's a great feeling when you've been working/struggling with something and finally get it working. And sometimes the best way is to start over like you did.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Glad to hear it and glad we could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't this program work???
 
Similar Threads
Calculator - Stacks
Array with Buttons"
help please?