Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-21-2008, 10:17 PM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
What's wrong with this code?
I am using the book Sams teach yourself Java 6 in 21 days and I'm stuck on the first code in Netbeans. I've tried everything I could think of but nothing works please help .

Code:
package firstproject; import java.util.*; public class Main { public static void main(String[] args) { String status; int speed; float Temperature; void checkTemperature() { if (Temperature > 660) { status = "returning home"; speed = 5; } } void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + Temperature); } } }
The lines underlined get this error:

illegal start of expression
';' expected


I've tried putting the ; everywhere but it still has that error.
Any help is welcome thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-21-2008, 10:36 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Your declaring methods inside of a method. Move them outside of the main method into the Main class and call them from the main method.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-21-2008, 10:45 PM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
K I thought I did that just now in netbeans but then it gave me more errors. How would I do that?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-21-2008, 11:37 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by ShoeNinja View Post
Your declaring methods inside of a method. Move them outside of the main method into the Main class and call them from the main method.

wusa, please re-read what ShoeNinja wrote.

If you have other errors that you did not mention in your first post as a result of doing what ShoeNinja said, do not just say you have "more errors". Post your updated code, using code tags along with the errors reported. We are not mind readers, even though sometimes some of us may think so - we need to see your results in order to help you.

And welcome to the Java Forums.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-21-2008, 11:42 PM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
Hmm k. I'm new to Java so I hadn't much of an idea what he was talking about so I did what sounded right to me but I guess it was wrong. So what I'm looking for is a more newbish kind of answer that new people would understand. I'll look at it again but NetBeans was being slow
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-22-2008, 01:49 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
I believe I sent you a PM and I realize it's still early, but I was hoping to see how you're making out. Feel free to post your code and specific errors. We're here to help.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-22-2008, 02:29 AM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
Ergh I know what I was doing was wrong but since you insist I'll post it.
I'm going to mess with it a bit more then if i still cant get it ill post my best attempt with the least errors.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-22-2008, 02:55 AM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
Ok good news and bad news, I got the errors gone BUT now when it runs the output is nothing. Why is this?

Heres my code now

Code:
package firstproject; import java.util.*; public class Main { public static void main(String[] args) {} String status; int speed; float Temperature; void checkTemperature() { if (Temperature > 660) { status = "returning home"; speed = 5; } } void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + Temperature); } }
I switched where i put the '}' bracket to fix the errors. But now like I said the programs output is nothing.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-22-2008, 03:27 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Okay, I see you followed ShoeNinja's advice and moved the functions outside of the main method. But now, your main method is simply this line:
Code:
public static void main(String[] args) {}


If the main method is only one line, with both the opening and closing brackets containing nothing in between, and main is the entry point of the application... how can you expect your program to output anything?

Please review the section on block scope from your textbook. Or, go to the end of this page and review the section on blocks.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 01-22-2008 at 03:31 AM.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-22-2008, 03:30 AM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
Hehe I thought it had something to do with that. Btw sorry for double post just noticed that.
Umm k I don't get any of that scope stuff. I think I'll get a different book for Java anyway since this book doesn't seem to like Java at all...

Last edited by Wizard wusa : 01-22-2008 at 03:32 AM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-22-2008, 03:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
public class Test { static String status; static int speed; static float Temperature; public static void main(String[] args) { Temperature = 250; checkTemperature(); showAttributes(); } static void checkTemperature() { if (Temperature > 660) { status = "returning home"; speed = 5; } } static void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + Temperature); } }
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-22-2008, 03:39 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Ill rewrite your updated code with some hints:

Code:
package firstproject; import java.util.*; public class Main { // entry point of the program public static void main(String[] args) {} // program starts and ends here String status; // instance variable of type String int speed; // instance variable of type int float Temperature; // instance variable of type float // method of the Main class, returns nothing void checkTemperature() { if (Temperature > 660) { status = "returning home"; speed = 5; } } // end of method checkTemperature // method of the Main class, returns nothing. void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + Temperature); } // end of method showAttributes } // end of class Main
Quote:
I switched where i put the '}' bracket to fix the errors. But now like I said the programs output is nothing.
Also, switching brackets around like a matching game does nothing for your understanding because you don't actually know why you're moving them around. Your book will have the answer you're looking for.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-22-2008, 03:47 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by Wizard wusa View Post
Hehe I thought it had something to do with that. Btw sorry for double post just noticed that.
Umm k I don't get any of that scope stuff. I think I'll get a different book for Java anyway since this book doesn't seem to like Java at all...
Definitely get another book then. Rushing this is not going to help you. As well as answers with no explanation such that Hardwired gave you. I was trying to assist you in learning the concept, rather than giving you the flat out answer.

Hardwired, please refrain from doing this - especially on active topics where another member or members are assisting someone in their understanding. If you're the first one to answer the thread, I can't hold you back from posting a complete answer even though I advise against it, but hijacking it with a solution without providing explanations is simply rude.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-22-2008, 03:56 AM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
K thank you and no it doesn't I copied the code exactly.
Hmm I think I got something going now I'm going to try to do something that people who know Java more would be like no don't do that it's a bad habit but I'm going to try it anyway and post what I did if it works(and I guess if it doesn't).

The one hardwired said didn't work anyway one value is returned a null the other 0 lol.

Last edited by Wizard wusa : 01-22-2008 at 04:07 AM.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-23-2008, 12:55 AM
Member
 
Join Date: Jan 2008
Posts: 10
Wizard wusa is on a distinguished road
I did it without using the extra methods I just condensed it all to the main method and it worked. No wonder I was confused by it the extra methods weren't needed... I was wondering why they were there.

EDIT: Oh whoops... I jumped on the problem to early it says later that it won't work even if its compiled correctly its added on to and used for something else later...

Last edited by Wizard wusa : 01-23-2008 at 01:06 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am Doing Something Wrong But Don't Know What? BHCluster New To Java 3 04-16-2008 02:16 PM
what is wrong with this code masaka New To Java 5 04-16-2008 09:27 AM
Image problems, what's wrong here. Bluefox815 Java Applets 1 03-07-2008 03:45 AM
Is there somethign wrong with this code? Soda New To Java 1 12-08-2007 05:46 PM
Whats wrong with my code??? Soda New To Java 2 12-06-2007 01:54 PM


All times are GMT +3. The time now is 09:40 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org