Error: No return statement

return statement error

Page 1 of 1

1 Replies - 3279 Views - Last Post: 06 October 2006 - 12:04 AM Rate Topic: -----

#1 Aristo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-December 05

Error: No return statement

Posted 05 October 2006 - 12:22 PM

Hello guys, I have prepared 2 class of Student and ArrayStudent and I am trying to calculated the average age of the total input at the last method of the ArrayStudent class...but I keep on getting the message (missing return statement) when I have one. Can anybody help please?

// Fig. 1.3: Student.java
// Student class definition
public class Student
{
	// A student's personal details
	private String name;
	private int age;

	// Student constructor initialises each instance variable
	public Student(String nameIn, int ageIn)
	{
		name = nameIn;
		age = ageIn;
	}

	// Return a student's name
	public String getName() {  return name; }

	// Return a student's age
	public int getAge()  { return age; }
}// end of class Student



// Fig. 1.3: ArrayStudent.java
// Application Class ArrayStudent is a testbed for class Student


import java.io.*;
public class ArrayStudent
{
	// keyboard input
	static BufferedReader keyboard = new BufferedReader(
									 new InputStreamReader(System.in));

	public static void main(String args[] )throws IOException
	{
		String name;
		int age, number;
		System.out.print("How many students? ");
		number = Integer.parseInt(keyboard.readLine());
		Student s[] = new Student[number];
		// input into the array
		for(int index = 0; index < number; index++)
		{
		  System.out.println();
		  System.out.print("Enter Name of student number " + (index+1) + "? ");
		  name = keyboard.readLine();
		  System.out.print("Enter Age for " + name + "? ");
		  age = Integer.parseInt(keyboard.readLine());
		  s[index] = new Student(name,age);
		}
		printArray(s);  
		
	}// end of main
	
	// Prints elements of array - "Student objects"
	private static void printArray(Student s[])
	{
		System.out.println("Student List:");
		for(int index = 0; index < s.length; index++)
		{
		  System.out.println(s[index].getName() + " " + s[index].getAge());
		}
	}// end of method printArray
	
	// Finding the average age.
	private static double findAverage(Student s[])
	{
		double sum = 0.0;
		for(int i = 0; i < s.length; i++)
		{
		   sum += s[i].getAge();
		   return sum / s.length;
		   }
		  System.out.println("The total average age is" + findAverage(s));
	}
}// end of class ArrayStudent



Is This A Good Question/Topic? 0
  • +

Replies To: Error: No return statement

#2 Java_N1ZJA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-August 06

Re: Error: No return statement

Posted 06 October 2006 - 12:04 AM

hey there

made the changes to your code..

firstly the return statement was in the for loop and should be placed after all calculation are done.

secondly you never technically called the findAverage() function because in your code your calling the function inside the function and this will cause an unreachable error.

but still good work just minor errors,happens all the time...

// Fig. 1.3: ArrayStudent.java
// Application Class ArrayStudent is a testbed for class Student


import java.io.*;
public class ArrayStudent
{
	// keyboard input
	static BufferedReader keyboard = new BufferedReader(
									 new InputStreamReader(System.in));

	public static void main(String args[] )throws IOException
	{
		String name;
		int age, number;
		System.out.print("How many students? ");
		number = Integer.parseInt(keyboard.readLine());
		Student s[] = new Student[number];
		// input into the array
		for(int index = 0; index < number; index++)
		{
		  System.out.println();
		  System.out.print("Enter Name of student number " + (index+1) + "? ");
		  name = keyboard.readLine();
		  System.out.print("Enter Age for " + name + "? ");
		  age = Integer.parseInt(keyboard.readLine());
		  s[index] = new Student(name,age);
		}
		printArray(s);  
		
	}// end of main
	
	// Prints elements of array - "Student objects"
	private static void printArray(Student s[])
	{
		System.out.println("Student List:");
		for(int index = 0; index < s.length; index++)
		{
		  System.out.println(s[index].getName() + " " + s[index].getAge());
		}
		System.out.println("The total average age is " + findAverage(s));
	}// end of method printArray
	
	// Finding the average age.
	private static double findAverage(Student s[])
	{
		double sum = 0.0;
		for(int i = 0; i < s.length; i++)
		{
		   sum += s[i].getAge();

		}
		return sum / s.length;		
	}
}// end of class ArrayStudent

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1