9 Replies - 669 Views - Last Post: 11 March 2013 - 04:52 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 10
  • View blog
  • Posts: 573
  • Joined: 10-March 13

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.

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



Is This A Good Question/Topic? 0
  • +

Replies To: Dont know how to change my main method

#2 unknown500  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 11-June 12

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.
Was This Post Helpful? 1
  • +
  • -

#3 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 10
  • View blog
  • Posts: 573
  • Joined: 10-March 13

Re: Dont know how to change my main method

Posted 10 March 2013 - 09:02 PM

View Postunknown500, 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.


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.
Was This Post Helpful? 0
  • +
  • -

#4 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 10
  • View blog
  • Posts: 573
  • Joined: 10-March 13

Re: Dont know how to change my main method

Posted 10 March 2013 - 09:14 PM

View Postunknown500, 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.


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.
Was This Post Helpful? 0
  • +
  • -

#5 unknown500  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 11-June 12

Re: Dont know how to change my main method

Posted 10 March 2013 - 10:03 PM

View Poststreek405, 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.


"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);

Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

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:

View Postunknown500, 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.
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

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

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


Was This Post Helpful? 1
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

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

Was This Post Helpful? 0
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,692
  • Joined: 13-March 10

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".
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Dont know how to change my main method

Posted 11 March 2013 - 04:52 PM

It is not a Java reserved word :tt2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1