import java.util.Scanner;
public class method {
public static main(String [] args) {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);
return sum;
}
}
Dont know how to change my main method
Page 1 of 19 Replies - 215 Views - Last Post: 11 March 2013 - 04:52 PM
#1
Dont know how to change my main method
Posted 10 March 2013 - 08:11 PM
Im trying to copy a Method example from my book, but it does not give me the entire code. I know that my 5th line is wrong, but I don't know how to fix it.
Replies To: Dont know how to change my main method
#2
Re: Dont know how to change my main method
Posted 10 March 2013 - 08:20 PM
You are very close. All you have to do is change your method header a bit:
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
#3
Re: Dont know how to change my main method
Posted 10 March 2013 - 09:02 PM
unknown500, on 10 March 2013 - 08:20 PM, said:
You are very close. All you have to do is change your method header a bit:
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
But I need to have a return variable for this example. I've tried it with public static void main(String [] args] but then it states that the return type does not match or something.
#4
Re: Dont know how to change my main method
Posted 10 March 2013 - 09:14 PM
unknown500, on 10 March 2013 - 08:20 PM, said:
You are very close. All you have to do is change your method header a bit:
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
Main methods in Java are written like this:
public static void main(String [ ] args)
Also, note that your main method should probably not return any variables.
Ignore my previous reply...you were right. So does that mean "public static void main(String [ ] args)" is similar to making an actual return type, if that makes any sense. What is the difference between it being void and declaring a return if they both are going to return something? Also, if I was to go about using the "return sum;" way, what would I have to change in my code?
Sorry for all of these questions, but I really am interested in this stuff and want to understand it thoroughly.
Thank you.
#5
Re: Dont know how to change my main method
Posted 10 March 2013 - 10:03 PM
streek405, on 10 March 2013 - 09:14 PM, said:
Ignore my previous reply...you were right. So does that mean "public static void main(String [ ] args)" is similar to making an actual return type, if that makes any sense. What is the difference between it being void and declaring a return if they both are going to return something? Also, if I was to go about using the "return sum;" way, what would I have to change in my code?
Sorry for all of these questions, but I really am interested in this stuff and want to understand it thoroughly.
Thank you.
Sorry for all of these questions, but I really am interested in this stuff and want to understand it thoroughly.
Thank you.
"What is the difference between it being void and declaring a return if they both are going to return something?"
Void methods do not return anything. Methods with return types (like int, double, or char) will return variables.
The main method must be void. The completion of the main method does not necessarily indicate the end of the program. This means that your program could exit sometime during the main method. Once the program exits, there would be no sense in returning anything. So, basically the method is void because you don't know when exactly the program will end.
"Also, if I was to go about using the "return sum;" way, what would I have to change in my code?"
That really depends on why you need to return sum. Do you need it elsewhere in your code? If not, it would be much easier to just print it out in the main method.
If you really need to return it, though, here is a work-around:
1. Create another static method that returns an int. This method should accept an int parameter. The body of the method should return that parameter.
private static int returnSum(int sumToReturn ){
return sumToReturn;
}
2. Call this method inside of your main method using your sum variable:
returnSum(sum);
#6
Re: Dont know how to change my main method
Posted 11 March 2013 - 02:13 AM
Nice post but I have to correct this part:
When you're looking to return a value, the convention is to use get. A "getter" doesn't need a method argument at all.
The time and place for method arguments is in setter methods.
So in this person's program they will want a get method to call in order to return the sum, it will need to be outside of main and called by main as follows.
unknown500, on 11 March 2013 - 05:03 AM, said:
If you really need to return it, though, here is a work-around:
1. Create another static method that returns an int. This method should accept an int parameter. The body of the method should return that parameter.
2. Call this method inside of your main method using your sum variable:
1. Create another static method that returns an int. This method should accept an int parameter. The body of the method should return that parameter.
private static int returnSum(int sumToReturn ){
return sumToReturn;
}
2. Call this method inside of your main method using your sum variable:
returnSum(sum);
When you're looking to return a value, the convention is to use get. A "getter" doesn't need a method argument at all.
int getValue() {
return value;
}
The time and place for method arguments is in setter methods.
void setValue(int valueToSet) {
this.value = valueToSet;
}
So in this person's program they will want a get method to call in order to return the sum, it will need to be outside of main and called by main as follows.
import java.util.Scanner; //This is unnecessary as you
//are not asking for user input
public class method {
public static main(String [] args) {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + getSum());
//call the method from inside main
}
//The method is outside of main()
int getSum() {
return sum;
}
}
This post has been edited by Flukeshot: 11 March 2013 - 02:16 AM
#7
Re: Dont know how to change my main method
Posted 11 March 2013 - 09:28 AM
and it will need to be static
and it will have to have a declared variable sum to return
and it will have to have a declared variable sum to return
import java.util.Scanner; //This is unnecessary as you
//are not asking for user input
public class method {
public static main(String [] args) {
System.out.println("Sum from 1 to 10 is " + getSum());
}
//The method is outside of main()
private static int getSum() {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
return sum;
}
}
#8
Re: Dont know how to change my main method
Posted 11 March 2013 - 04:11 PM
And we both forgot to set our main return types to void 
public class method {
public static void main(String [] args) {
System.out.println("Sum from 1 to 10 is " + getSum());
}
private static int getSum() {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
return sum;
}
}
#9
Re: Dont know how to change my main method
Posted 11 March 2013 - 04:43 PM
why the hell would you name a class "method".
#10
Re: Dont know how to change my main method
Posted 11 March 2013 - 04:52 PM
It is not a Java reserved word
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|