3 Replies - 917 Views - Last Post: 13 November 2011 - 07:40 PM Rate Topic: -----

#1 Stamati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-October 11

Error: missing return statement?

Posted 13 November 2011 - 05:59 PM

public class ArrayProcessor
{
	private int count;
	private int[] myArray;
	
	public ArrayProcessor (int maxSize)
	{
		count = 0;
		myArray = new int[maxSize];
	}
	
	public void add (int userValue)
	{
		
		if (count == myArray.length){
			
		}
		else
			for (int i=0; i < myArray.length; i++)
				myArray[i] = userValue;
				count++;
	}
	
	public int max()
	{
		int max = 0;
		max = myArray[0];
		for (int i=0; i < myArray.length; i++)
			if (myArray[i] > max)
				max = myArray[i];
				
		return max;
		
	}
	
	public int min()
	{
		int min = 0;
		min = myArray[0];
		for (int i=0; i < myArray.length; i++)
			if (myArray[i] < min)
				min = myArray[i];
				
		return min;
		
	}
	
	public int sum()
	{
		int sum = 0;
		for (int i=0; i < myArray.length; i++)
			sum += myArray[i];
			
		return sum;
		
	}
	
	public double average()
	{
		double average = 0;
		average = sum() / myArray.length;
		
		return average;
	}
	
	public int search (int target)
	{
		for (int i=0; i < myArray.length; i++)
			if (myArray[i] == target)
				
			return myArray[i];
	}                                               	
	public int howMany (int target)
	{	
		int counter = 0;
		for (int i=0; i < myArray.length; i++){
			if (myArray[i] == target)
				counter++;
		}
				
		return counter;
				
	}
}


I keep getting an error statement ArrayProcessor.java:72: missing return statement
}
^
I dont know whats up, it looks correct to me.

Is This A Good Question/Topic? 0
  • +

Replies To: Error: missing return statement?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Error: missing return statement?

Posted 13 November 2011 - 06:02 PM

What is returned if the 'if' statement is false?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10684
  • View blog
  • Posts: 18,298
  • Joined: 19-March 11

Re: Error: missing return statement?

Posted 13 November 2011 - 06:02 PM

	    public int search (int target)
	    {
	        for (int i=0; i < myArray.length; i++)
	            if (myArray[i] == target)
	                 
	            return myArray[i];
	    }               


What happens if this if statement is true on each turn through the loop?

This is one of those cases where using curly braces on every statement block would be useful.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error: missing return statement?

Posted 13 November 2011 - 07:40 PM

Proper code identation would a show you the probem identified by GregBrannon and jon.kiparsky
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1