Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tullamore

Thread: Question about adding a method. Help!

  1. #1
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Unhappy Question about adding a method. Help!

    So...I have this code that does what I need it to. But it has to contain a getDistance method. I've tried several times but get an error message. Any help is much appreciated. Thanks. Here it is:

    Java Code:
    import java.util.Scanner;
    public class distanceTraveled
    {
    	public static void main(String[] args)
    	{
    		int speed;
    		int hours;
    		int distance;
    		String input;
        	Scanner keyboard= new Scanner(System.in);
    
            System.out.print("Enter speed of vehicle in mph: ");
        	speed= keyboard.nextInt();
    
        	if(speed> 0)
        	{
        		System.out.print("Enter the # of hours you have traveled: ");
        		hours= keyboard.nextInt();
    
        		if(hours>= 1)
        		{
        			System.out.println("Hours      Distance traveled");
        			System.out.print("------------------------------\n");
    
        			for(int a= 1; a<= hours; a++)
        			{
        				distance=(speed * a);
        				System.out.println(a+ "               " + distance);
        			}
        		}
    
                else System.out.println("The # of hours traveled must be at least 1");
        	    }
        	    else System.out.println("Speed must be a positive number");
                System.exit(0);
        }
    }
    Last edited by Fubarable; 11-05-2011 at 05:32 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question about adding a method. Help!

    Quote Originally Posted by Tullamore View Post
    So...I have this code that does what I need it to. But it has to contain a getDistance method. I've tried several times but get an error message. Any help is much appreciated. Thanks. Here it is:

    Java Code:
    import java.util.Scanner;
    public class distanceTraveled
    {
    	public static void main(String[] args)
    	{
    		int speed;
    		int hours;
    		int distance;
    		String input;
        	Scanner keyboard= new Scanner(System.in);
    
            System.out.print("Enter speed of vehicle in mph: ");
        	speed= keyboard.nextInt();
    
        	if(speed> 0)
        	{
        		System.out.print("Enter the # of hours you have traveled: ");
        		hours= keyboard.nextInt();
    
        		if(hours>= 1)
        		{
        			System.out.println("Hours      Distance traveled");
        			System.out.print("------------------------------\n");
    
        			for(int a= 1; a<= hours; a++)
        			{
        				distance=(speed * a);
        				System.out.println(a+ "               " + distance);
        			}
        		}
    
                else System.out.println("The # of hours traveled must be at least 1");
        	    }
        	    else System.out.println("Speed must be a positive number");
                System.exit(0);
        }
    }
    Please show us your best attempt to create this method and any and all error messages that it may generate.

    Also, I've added [code] [/code] tags to your post above.

  3. #3
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Basically, After the public static void main method...I try to insert my getDistance method. like so:

    public static void main(String[] args
    {
    public getDistance(int speed, int hours, int distance)
    {
    String input;

    .....like so. I'm not looking for the exact answer, just a few hints or tips that will help me get this done. I'm just drawing a blank. Thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question about adding a method. Help!

    Again, please let's see your full attempt and your full error messages. How can we tell what you're doing wrong without this information?
    Again, please use code tags when posting code.

  5. #5
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Alright....sorry. New here. I'll do that and post what I got. Thanks for the super timely response by the way.
    Fubarable likes this.

  6. #6
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Alright...here's the code:

    Java Code:
    import java.util.Scanner;
    public class distanceTraveled
    {
    	public static void main(String[] args)
    	{
    		public getDistance(int speed, int hours, int distance)
    		{
    
    			String input;
        		Scanner keyboard= new Scanner(System.in);
    
            System.out.print("Enter speed of vehicle in mph: ");
        	speed= keyboard.nextInt();
    
        	if(speed> 0)
        	{
        		System.out.print("Enter the # of hours you have traveled: ");
        		hours= keyboard.nextInt();
    
        		if(hours>= 1)
        		{
        			System.out.println("Hours      Distance traveled");
        			System.out.print("------------------------------\n");
    
        			for(int a= 1; a<= hours; a++)
        			{
        				distance=(speed * a);
        				System.out.println(a+ "               " + distance);
        			}
        		}
    
                else System.out.println("The # of hours traveled must be at least 1");
        	    }
        	    else System.out.println("Speed must be a positive number");
                System.exit(0);
    		}
        }
    }
    And these are the errors I get:

    error: illegal start of expression line 7
    error: '.class' expected line 7
    error: ';' expected line 7
    error: <identifier> expected line 7
    error: not a statement line 7
    error: ';' expected line 7

    I'm using JCreator too if that helps at all.

  7. #7
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Actually...all the errors are pertaining to the public getDistance line, which is 6.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question about adding a method. Help!

    OK, you'll want to check your textbook or tutorial about writing methods. Things to know: methods cannot be nested inside of other methods (i.e., you can't nest your new method inside of the main method). Next, methods must declare a return type or void. Next, your method for this program will need to be static... But these are all things that your text will tell you.

    Good luck.

  9. #9
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Alright. I have been staring through my textbook, and I'm just not getting it. I tried this:

    Java Code:
    //PC.2
    import java.util.Scanner;
    public class distanceTraveled
    {
    	public int getDistance(int speed, int hours, int distance)
    	{
    
    		String input;
        	Scanner keyboard= new Scanner(System.in);
    
            System.out.print("Enter speed of vehicle in mph: ");
        	speed= keyboard.nextInt();
    
        	if(speed> 0)
        	{
        		System.out.print("Enter the # of hours you have traveled: ");
        		hours= keyboard.nextInt();
    
        		if(hours>= 1)
        		{
        			System.out.println("Hours      Distance traveled");
        			System.out.print("------------------------------\n");
    
        			for(int a= 1; a<= hours; a++)
        			{
        				distance=(speed * a);
        				System.out.println(a+ "               " + distance);
        			}
        		}
    
                else System.out.println("The # of hours traveled must be at least 1");
        	    }
        	    else System.out.println("Speed must be a positive number");
                System.exit(0);
    		}
    }
    With this, I only get this error:

    error: missing return statement line 35

    Is the static void main method necessary? Is the problem with my variable declaration? I'm just not getting how to proceed, after looking over the textbook. Again, I appreciate your help..ALOT! Thanks.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question about adding a method. Help!

    Quote Originally Posted by Tullamore View Post
    Alright. I have been staring through my textbook, and I'm just not getting it. I tried this:

    Java Code:
    //PC.2
    import java.util.Scanner;
    public class distanceTraveled
    {
    	public int getDistance(int speed, int hours, int distance)
    	{
    
    		String input;
        	Scanner keyboard= new Scanner(System.in);
    
            System.out.print("Enter speed of vehicle in mph: ");
        	speed= keyboard.nextInt();
    
        	if(speed> 0)
        	{
        		System.out.print("Enter the # of hours you have traveled: ");
        		hours= keyboard.nextInt();
    
        		if(hours>= 1)
        		{
        			System.out.println("Hours      Distance traveled");
        			System.out.print("------------------------------\n");
    
        			for(int a= 1; a<= hours; a++)
        			{
        				distance=(speed * a);
        				System.out.println(a+ "               " + distance);
        			}
        		}
    
                else System.out.println("The # of hours traveled must be at least 1");
        	    }
        	    else System.out.println("Speed must be a positive number");
                System.exit(0);
    		}
    }
    With this, I only get this error:

    error: missing return statement line 35
    The method states that it will return an int, and so the method must in fact return an int, no matter what. Please have a look at Defining Methods.


    Is the static void main method necessary?
    Yes, if you want your program to run. That's the method that the Java engine first looks for and first calls to start your program. Without at least one viable main method, your class may be compilable but won't be runnable (with a few exceptions that don't count here).


    Is the problem with my variable declaration? I'm just not getting how to proceed, after looking over the textbook. Again, I appreciate your help..ALOT! Thanks.
    Keep looking, keep studying since your problems are one that will be mainly fixed by study, not by a forum. And don't have any System.out.println(...) methods inside your getDistance method. This method isn't supposed to display anything but rather it's supposed to return a value to the main method.

  11. #11
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Question about adding a method. Help!

    Alright. Thanks for your help. I'll keep plugging away.

Similar Threads

  1. Replies: 17
    Last Post: 07-19-2011, 01:53 AM
  2. Help required with complex method: adding item
    By nignogs in forum New To Java
    Replies: 14
    Last Post: 05-11-2011, 02:06 PM
  3. Replies: 1
    Last Post: 12-17-2010, 10:24 AM
  4. Replies: 3
    Last Post: 11-05-2010, 11:31 AM
  5. Method for adding and drawing several objects
    By Pillow in forum New To Java
    Replies: 2
    Last Post: 09-09-2010, 12:24 AM

Posting Permissions

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