Results 1 to 15 of 15
  1. #1
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default 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 :eek: .

    Java Code:
    package firstproject;
    
    import java.util.*;
    
    public class Main {
    
    
        public static void main(String[] args) {
    
            String status;
    	int speed;
    	float Temperature;
    
    [U]	void checkTemperature() {
    [/U]		if (Temperature > 660) {
    			status = "returning home";
    			speed = 5;
    		}
    	}
    [U]	void showAttributes() {
    [/U]		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!

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  3. #3
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    K I thought I did that just now in netbeans but then it gave me more errors. How would I do that?

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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. ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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 :(

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  7. #7
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  8. #8
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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

    Java Code:
    package firstproject;
    
    import java.util.*;
    
    public class Main {
    
    
        public static void main(String[] args) {[COLOR="Red"]}[/COLOR]
            
    	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);
    	}
    
    
        }
    
    [COLOR="Red"]
    [/COLOR]
    I switched where i put the '}' bracket to fix the errors. But now like I said the programs output is nothing.

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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:
    Java Code:
        public static void main(String[] args) {[COLOR=Red]}[/COLOR]


    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.
    Last edited by CaptainMorgan; 01-22-2008 at 03:31 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  10. #10
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }

  12. #12
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Ill rewrite your updated code with some hints:

    Java Code:
    package firstproject;
    
    import java.util.*;
    
    public class Main {
    
        // entry point of the program
        public static void [I]main[/I](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 [B]Main[/B] class, returns nothing
        void checkTemperature() {
            if (Temperature > 660) {
                status = "returning home";
                speed = 5;
            }
        } // end of method checkTemperature
    
        // method of the [B]Main[/B] 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 [B]Main[/B]
    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  13. #13
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  14. #14
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  15. #15
    Wizard wusa is offline Member
    Join Date
    Jan 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •