Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Program working fine until I add methods....

    This program was working fine until I added the findSum method. It is for one of my Uni classes, so I need to put one in. The error I keep getting is

    javac PercentOfSum.java
    PercentOfSum.java:71: missing return statement
    }
    ^
    1 error

    There isn't suppose to be a return statement there, so the problem must be somewhere else, most likely in the method. Im not to good at writing them, so I can see that being it.

    I dont need the answer, just a push in the right direction
    Thanks in advance




    Java Code:
    import java.util.*;
    
    public class PercentOfSum
    {
    
    	public static void main(String[] args)
    	{
    		System.out.println("KH");
    		System.out.println("ID ");
    		System.out.println("This program takes n amount of numbers,");
    		System.out.println("finds their sum and what percent they are of the sum");
    		System.out.println("November 30 2010");
    	
    		
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("How many numbers will you enter?");
    		//number of numbers in array
    		int numOfNum = keyboard.nextInt();
    		//makes array with that many spaces
    		int[] num2 = new int[numOfNum];
    		System.out.println("Enter " + numOfNum + " integers ,one per line: ");
    
    	}
    			public static int findSum(int[] num2,int numOfNum)
    				{
    					Scanner keyboard = new Scanner(System.in);	
    					int sum = 0;
    					//for loop to ask for numbers
    					for(int i=1; i <= numOfNum; i++)
    					{
    			
    						System.out.println("Entre number " + i + "." );	
    						//saves numbers in array
    						num2[i-1] = keyboard.nextInt();
    						//method
    						sum = (num2[i-1] + sum);
    		
    		
    				    return sum; 		
    				}
    				
    		
    			System.out.println();
    			System.out.println("You have filled the array with " + numOfNum + " numbers.");
    			System.out.println();
    			sum=findSum(num2,numOfNum);
    			System.out.println("The Sum is " + sum);
    			System.out.println();
    
    			
    
    
    		
    			System.out.println("Here are the numbers and their ");
    			System.out.println("percent contribution to the sum");
    			//change sum from int to double
    			double doubeSum = sum;
    		//loop to find percent contribution
    		for(int i=1; i <= numOfNum; i++)
    		{
    			double percent;
    			percent = 100.0*num2[i-1]/sum;
    			System.out.println(num2[i-1] + ", which is " +Math.round(percent)+ " % of the sum.");
    	
    }
    		}
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    All methods that have a return type (in your case, "int") have to return something on every control path. Usually if it is invalid, you can return -1 or some other garbage value instead.
    What I noticed is that everything after your first "for" loop should be in the main method instead of the findSum method. Then, you can safely return a value from OUTside the for loop instead of inside it.

  3. #3
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    You mean I need another }
    Before the return?

    when I do that I get
    49 errors, quite a few which make no sense

  4. #4
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    no basically you have to have a return statement before the closing brace for your function that has a return type.

    Java Code:
    for(int i=1; i <= numOfNum; i++)
    					{
    			
    						System.out.println("Entre number " + i + "." );	
    						//saves numbers in array
    						num2[i-1] = keyboard.nextInt();
    						//method
    						sum = (num2[i-1] + sum);
    		
    		
    				    return sum; 		
    				}
    this return statment is inside the body of loop if the loop doesn't get executed, if your user enters numOfNum as 0 than obviously i<=0 is false and the loop never gets executed. so at end of your function you need to have a return statment before the closing }.

Similar Threads

  1. Working with Methods Can anyone help?
    By RanLineR in forum New To Java
    Replies: 36
    Last Post: 10-16-2010, 04:01 AM
  2. Working with Methods
    By besweeet in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 06:33 PM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 2
    Last Post: 03-16-2009, 08:52 PM
  5. Static methods - not working
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 02:31 PM

Posting Permissions

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