Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Error using public static double Method

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error using public static double Method

    Hi everyone,
    I have a Java program that's supposed to convert feet to meters and meters to feet using methods.
    Here's what I have:

    public class HW3_1Method {
      public static void main(String[] args) {
     
       System.out.println("Feet      Meters");
       System.out.println("---------------------");
     
       System.out.println("Meters      Feet");
       System.out.println("---------------------");
     
       /**Converts from feet to meters*/
      //14  public static double footToMeter(double foot) {
       double meter = (.305 * foot) ;
       for(foot = 1; foot <=5; foot++) 
       System.out.println("      "  + foot + "      " + meter);
           }
     
       /**Converts from meter to feet*/
       //21 public static double meterToFoot(double meter) {
       double foot = (3.28 * meter);
       for(meter = 1; meter <=5; meter++) 
       System.out.println("      "  + meter + "      " + foot);
       }
    }
    //27 }

    There are errors on lines 14, 21 and 27 (I commented these lines). On those lines, it hints I'm missing return statements? When I try to run, I get the error using NetBeans:


    run:
    Feet Meters
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
    ---------------------
    Meters Feet
    ---------------------
    at HW3_1Method.main(HW3_1Method.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)


    Any help would be great, thank you!
    Last edited by stommy989; October 12th, 2010 at 08:54 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Error using public static double Method

    you forgot to end your main method with a curly brace, you've also forgotten to put return statements inside of your methods.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error using public static double Method

    Thank you...dumb question, but how should the return statement look inside the method? I'm sorry, I'm a newbie haha.

    Sam

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Error using public static double Method

    public class HW3_1Method 
    {
    	public static void main(String[] args) 
    	{
     
    	   	System.out.println("Feet      Meters");
    		System.out.println("---------------------");
     
    		System.out.println("Meters      Feet");
       		System.out.println("---------------------");
     
    	//We need to close our main method before we can make other methods
    	[b]}[/b]
       	/**Converts from feet to meters*/
      	public static double footToMeter(double foot) 
    	{
       		double meter = (.305 * foot) ;
       		for(foot = 1; foot <=5; foot++)
       			System.out.println("      "  + foot + "      " + meter);
    		//You need to return the same type as what you have indicated in your method declaration
    		[b]return meter;[/b]
           	}
     
       	/**Converts from meter to feet*/
       	public static double meterToFoot(double meter) 
    	{
       		double foot = (3.28 * meter);
       		for(meter = 1; meter <=5; meter++)
       			System.out.println("      "  + meter + "      " + foot);
    		[b]return foot;[/b]
       	}
    }
    (I used code tags instead of java tags for this so I could illustrate the additions)

    I have bolded what needs to be added for your code to be correct. Like helloworld922 said, you need to close your main method before you can make your other methods.

    You also need to remember that you have to return whatever type or object you have declared in your method. When you design your methods, you need to decide if you will return nothing or something. If you return nothing, you declare the return type to be void. If you want to return something, you have to indicate what you will be returning and you need to return the correct type or object.

    Here are some examples:
    //By saying "void", we have indicated that we are not going to return anything
    public void method()
    {...}
     
    //By saying "double", we have indicated that we are going to return a double. Which we do as the last statement in the method body.
    public double method()
    {
    return 10.9;
    }
     
    //By saying "JButton", we have indicated that we are going to return a JButton Object. Which we do as the last statement in the method body.
    public JButton method()
    {
    return new JButton("This is a JButton");
    }

    The last discrepancy with methods (and variables) is whether or not they need to be static. Basically, if the method or variable is called from within static content (like the main method), it needs to be static. Until you start making your own classes, make all of your methods and Global Variables in your main class static.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error using public static double Method

    Hmm, what's strange is now the build is successful, but no values are returned even though void isn't used.


    run:
    Feet Meters
    ---------------------

    Meters Feet
    ---------------------
    BUILD SUCCESSFUL (total time: 0 seconds)


    It's supposed to return the table of values within the table...How could I do a return in the main method, if that's the right question to be asking?
    Last edited by stommy989; October 13th, 2010 at 02:40 PM.

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Error using public static double Method

    return doesn't print you have to print/

    public String getMessage(){
       return "Hello!"
    }
    //...main method and other code
    System.out.println(getMessage());

Similar Threads

  1. Public class help/error
    By Plural in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 05:22 PM
  2. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  3. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  4. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM