• 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

Unreachable Statement

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a little trouble figuring this one out. Basically, I am reading Head First Java and I am trying to write a method that
gets some user input, but if the user input doesn't match one of the Strings in an ArrayList, then to return "invalid". I am just a beginner, so the code closely resembles the code given in chapter 5 of the book for getting user input.



basically, this won't compile and it says that the first "return "invalid"" line is unreachable.. I'ts a problem I can get around, but I was just wondering why the compiler thinks this is unreachable. I mean, it's just a simple if/else, right? If I get rid of the second return invalid line, it complies fine though. Not a big problem, just trying to understand how the method is compiling. Shouldn't it not care that there are two lines that say return "invalid"? If that makes any sense.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler is smart enough to recognize one of the return statement in if-else must be execute! so the statements after else block can not be reached thus the compiler throw an error!

use decompiler for below code. you can get some light!

 
Ranch Hand
Posts: 51
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to remove the else block or you have to comment the last return statement.(line#26)

 
Ranch Hand
Posts: 41
Google Web Toolkit Tomcat Server Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the code will terminate in the if or the else statement, it will never go beyond the else block. Hence unreachable.


For eg if you change the else with an else if (something) you can write the last return "invalid"

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Benjamin, what compiler are you using? Because with Oracle's JDK 1.6.0_26 on my system, it's the second return "invalid";" statement that's causing the error, not the first.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Benjamin, what compiler are you using? Because with Oracle's JDK 1.6.0_26 on my system, it's the second return "invalid";" statement that's causing the error, not the first.


Indeed! for me too .
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the JDK 1.7.0. Just got it! Anyway, this just had me scratching my head is all. What's weird is I just tried it again, and it compiles fine now...same code and everything! Really strange. (Ofc I added the approperate import statements, but I think other than that it is the same code word for word..) I have no idea.
reply
    Bookmark Topic Watch Topic
  • New Topic