• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Missing return statement

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just an else at the end would have sorted it out. But there are much better ways to go it.
 
Darragh Bourke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It Worked, thank you very much Campbell Ritchie and Guido Sautter i'd be lost without you two!!!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic