|
|
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.
|
|

01-21-2008, 10:17 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
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  .
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!
|
|

01-21-2008, 10:36 PM
|
 |
Senior Member
|
|
Join Date: Oct 2007
Posts: 112
|
|
|
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.
|
|

01-21-2008, 10:45 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
K I thought I did that just now in netbeans but then it gave me more errors. How would I do that?
|
|

01-21-2008, 11:37 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
Originally Posted by 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.
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)
|
|

01-21-2008, 11:42 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
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 
|
|

01-22-2008, 01:49 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
|
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)
|
|

01-22-2008, 02:29 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
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.
|
|

01-22-2008, 02:55 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
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
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.
|
|

01-22-2008, 03:27 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
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:
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.
|
|

01-22-2008, 03:30 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
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.
|
|

01-22-2008, 03:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,146
|
|
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);
}
}
|
|

01-22-2008, 03:39 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
Ill rewrite your updated code with some hints:
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
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)
|
|

01-22-2008, 03:47 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
Originally Posted by Wizard wusa
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)
|
|

01-22-2008, 03:56 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
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.
|
|

01-23-2008, 12:55 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|