my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Missing return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Missing return statement" Watch "Missing return statement" New topic
Author

Missing return statement

Darragh Bourke
Greenhorn

Joined: Apr 12, 2008
Posts: 6
Hi just wondering if anyone could help me with this little problem:

import javax.swing.JOptionPane;

class Exam{
String name;
String location;
int day;
int month;
int year;

public Exam(String n, String loc, int d, int m, int y){
name = n;
location = loc;
day = d;
month = m;
year = y;
}
public String toString(){
return name+": "+location+" "+day+"/"+month+"/"+year;
}
public boolean compareTo(Exam other){
if (year < other.year) {return true;}
if (year > other.year) {return false;}

if (month < other.month) {return true;}
if (month > other.month) {return false;}

if (day < other.day) {return true;}
if (day >= other.day) {return false;}
}
}
public class examA
{
public static void main(String[] args)
{

Exam[] examArray = new Exam[5];
for(int i = 0; i < examArray.length; i++){
String name = JOptionPane.showInputDialog(null,"enter name");
String loc = JOptionPane.showInputDialog(null,"enter location");
int day = Integer.parseInt(JOptionPane.showInputDialog(null,"enter day"));
int month = Integer.parseInt(JOptionPane.showInputDialog(null,"enter month"));
int year = Integer.parseInt(JOptionPane.showInputDialog(null,"enter month"));
examArray[i] = new Exam(name,loc, day, month,year);
}
int lowest_loc = 0;
for(int m = 1; m < examArray.length; m++){
for(int i = 1; i < examArray.length; i++){
if(examArray[i] != null){
if (examArray[i].compareTo(examArray[lowest_loc])) {
lowest_loc = i;
}
}
}
JOptionPane.showMessageDialog(null,examArray[lowest_loc]);

}

System.exit(0);
}
}

examA.java:29: missing return statement
}
^
1 error

Doing this for revision as I have an exam tomorrow!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Please use code tags, and mark the line with the missing return statement.

What you are trying to do is to return whether the date (day, month, year) are different, and you have a potential path through the method (at least in the eyes of the compiler) which returns neither true nor false. You will have to put something in for when all the ifs are missed out. The compiler is not up to working out that the two ifs for day cover all possibilities.

BTW: Read the Sun Coding Conventions, especially �10.5.2 for returning boolean values.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Try putting up an if - else if - ... - else if - else chain instead of the individual if statements. This is something the compiler can figure out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Just an else at the end would have sorted it out. But there are much better ways to go it.
Darragh Bourke
Greenhorn

Joined: Apr 12, 2008
Posts: 6
It Worked, thank you very much Campbell Ritchie and Guido Sautter i'd be lost without you two!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
but this would have worked better . . .

return year < other.year || month < other.month || day < other.day;

BTW: If you look up the Comparable<T> interface you find the compareTo method is supposed to be used for something completely different, so it would have been better to call that method something different, eg isOlderExam()
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Oh, I am sorry, I was in a rush this morning and posted too simple a method.

Try


[ May 09, 2008: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Missing return statement