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:
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);
}
}
Re: Question about adding a method. Help!
Quote:
Originally Posted by
Tullamore
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:
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.
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.
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.
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.
Re: Question about adding a method. Help!
Alright...here's the code:
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.
Re: Question about adding a method. Help!
Actually...all the errors are pertaining to the public getDistance line, which is 6.
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.
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:
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. :)-:
Re: Question about adding a method. Help!
Quote:
Originally Posted by
Tullamore
Alright. I have been staring through my textbook, and I'm just not getting it. I tried this:
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.
Quote:
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).
Quote:
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.
Re: Question about adding a method. Help!
Alright. Thanks for your help. I'll keep plugging away.