wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes unreachable statements. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unreachable statements." Watch "unreachable statements." New topic
Author

unreachable statements.

Mario Martinez
Greenhorn

Joined: Oct 10, 2002
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
Please write some reasonable java code.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.


Dave
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Please use the "code" tages so we read what you have.
Do you increment "i" anywhere?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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

Joined: Oct 10, 2002
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Oct 10, 2002
Posts: 12
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

Joined: Mar 16, 2001
Posts: 986

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Oct 10, 2002
Posts: 12

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

Joined: Aug 03, 2002
Posts: 7729
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unreachable statements.