Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unreachable statements.

 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I try to have more than one return in a method, I get an unreachable statement. One of the returns depends if the if statement is true, so shouldnt it return only on of the returns?
And, how can I return a null object, eg:
private SomeName name (String getName) {
for(i=0 i < arraysize)
{..... get array i
if (getName equals array i)
return object;
}

return null; //unreachable statement. How can I return a null if the former if after the for loop finishes, and the if statement is still false?
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please write some reasonable java code.
-Barry
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario
Barry is right, we need to see the actual code you have. If the compiler is complaining, it is because you have it coded in such a way that the last return is not reachable from any possible execution path. Let us see the actual code and then we can figure it out for you.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the "code" tages so we read what you have.
Do you increment "i" anywhere?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I return a null if the former if after the for loop finishes, and the if statement is still false?

Isn't that implied by getting out of the loop?
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hahah your right im just so stressed one little bug has been bothering me all week. Anyway after fiddling around for the 426th time I actually got rid of the error.
private TeamData FindTeam(String teamName) {
//

This is what FindTeam is suppose to find, the teams in the text file. If the team is already contained in the arraylist, then just update it but if the team hasnt been added to the arraylist, then return a null here is a sample test file.
But as you can see, there is noreunnerup team for the Cornel, so I am getting a nullsuchelement exception. So I figure if I make a statement, that if the tokenizer is a null, it should just skip it, and continue to read the next one. So I did, and it doesnt read the null after Cornel, BUT! Northwestern played itself and my catch statement catches it, but I get an error when I try to sort the teams using collections.sort
NCAA Men's Basketball Tournament Archive
Please enter the tournament data file.. eg ncaa-test.data, ncaa2001.data, etc.
ncaa-test.data.txt
File format error14
1944:Cornell
Error: Northwestern couldn't have played itself19
1990:Northwestern:Northwestern
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at TournamentArchive$CompareTeamsByName.compare(TournamentArchive.java:1
60)
at java.util.Arrays.mergeSort(Arrays.java:1241)
at java.util.Arrays.mergeSort(Arrays.java:1248)
at java.util.Arrays.mergeSort(Arrays.java:1248)
at java.util.Arrays.sort(Arrays.java:1188)
at java.util.Collections.sort(Collections.java:153)
at TournamentArchive.SortTeamList(TournamentArchive.java:205)
at TournamentArchive.main(TournamentArchive.java:324)
Press any key to continue...
[ October 11, 2002: Message edited by: Dirk Schreckmann ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody, please tell this guy about the misplaced
semicolons.
I have been trying to tell him all day...here

[ October 11, 2002: Message edited by: Barry Gaunt ]
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know but, I get a missing return statement when I take the semicolon out
EDIT, oh, the bracket was suppose to be in the return statement thats why im such a clutz lol
thanks alot
[ October 11, 2002: Message edited by: Mario Martinez ]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem is that your 'return null;' is inside of your for loop. So it will get executed on the very first iteration. It seems like you want it outside of the loop. That was if the looop ends without 'return'ing you than need to return null.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So 1) your for loop whizzed around doing nuffink,
and 2) your if don't do nuffink either.
HoHum... I'll think it's time for some Hunny...
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You guys are great thank you so much!
Even though ill probobly will never be able to run this god forsaken program, at least I got close to doing it lol
[ October 11, 2002: Message edited by: Mario Martinez ]
[ October 11, 2002: Message edited by: Mario Martinez ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, let's optimize things a little.
For to be in that for loop you must
have i < teamList.size().
So why do you need the if( i < teamList.size())?
The thing in the {} after the if is always going to be done.
So all you need is:
Which is about what you started out with a couple of days ago (less a couple of semicolons )
[ October 11, 2002: Message edited by: Barry Gaunt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic