*
The moose likes Java in General and the fly likes Unreachable statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unreachable statement" Watch "Unreachable statement" New topic
Author

Unreachable statement

Azhar Karim
Greenhorn

Joined: Feb 13, 2013
Posts: 10
My program has an error which says unreachable error.

total0=principle0+calcInterest;
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Hi Azhar. Welcome to the Ranch!

If the compiler says that statement is unreachable, then the problem is actually further up the method. The compiler can tell that there's no possible way of reaching that line. For example, if the method will always have returned before it reaches that line.

If it's not clear from that explanation, show us the preceding code and we can probably help.
Azhar Karim
Greenhorn

Joined: Feb 13, 2013
Posts: 10
Is there anyway can I attach photos here?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

You can upload attachments, or link to an image elsewhere. But it's usually much easier for people to read if you paste the code direct (making sure you format it and UseCodeTags - see the link for details).
Azhar Karim
Greenhorn

Joined: Feb 13, 2013
Posts: 10
Azhar Karim
Greenhorn

Joined: Feb 13, 2013
Posts: 10
is this fine?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

I did say UseCodeTags (<- read the link) - I've added them for you this time.

OK, your problem is on line 89, right? Just before that you've got a break statement (line 87). As soon as the execution hits that it leaves the switch statement. So lines 89 to 98 cannot possibly be executed. The compiler realises this, assumes you've made a mistake (because why would you add code that can't be executed?) and gives an error.

(Edited to match altered line-numbers)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Matthew Brown wrote:I did say UseCodeTags (<- read the link) - I've added them for you this time. . . .
So did I, and broke some of those very long lines, which make the code difficult to read.
Apart from those long lines, there are other style problems.
  • Don’t declare more than one variable at a time.
  • Only write one line between case xxx: and break; It is better to call another method than have a long block of code there.
  • Your main method is much much too long.
  • Your print instructions appear repetitive. You can probably create a printMessage() method which does all the printing.
  • Why are you using DecimalFormat rather than the % tags?
  • Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    What am I supposed to do now? I need to send this by tomorrow to my teacher
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3432
        
      12
    Azhar Karim wrote:What am I supposed to do now? I need to send this by tomorrow to my teacher

    You either need to move the break statement to after line 98 or move lines 89-98 to somewhere else (or delete them if they're not required). At the moment, even if the compiler allowed them to be there, they would never be executed.
    Nobody here knows what your program is supposed to do, so only you can decide which is the best option.

    Joanne
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    You have lots of time to implement improvements.
    Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    I cant delete them since i need to store the inputs and results in array. When i moved the break statement, errors saying that e.g interest1 might not be initialized
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4344
        
        8

    Azhar Karim wrote:I cant delete them since i need to store the inputs and results in array. When i moved the break statement, errors saying that e.g interest1 might not be initialized


    That's because you don't initialise interest1 in case 3. It doesn't have a value, so you can't use that value later. You initialise it on case 2 (line 59), but if they've selected 3 that never happens.
    Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    the error is not only for interest1. All the other inputs and results will have errors saying it's not initialized
    Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 4447
        
        5

    You need to initialize them all. Also, you don't need that many variables since the calculations for each option chosen by the user is done independently of the others. I would just use one set of variables for all the options.


    Junilu - [How to Ask Questions] [How to Answer Questions]
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11175
        
      16

    Then initialize them.

    Personally, my advice would be to throw this away and start over. I NEVER NEVER NEVER write more than 2-3 lines of code before I compile.

    It looks like you wrote roughly 150. There are bound to be syntax errors all over the place, and often some will mask others...so it tells you it found one error, and you fix it. You then re-compile, and now the parser can get a little further, and it finds another error. You fix that, it finds another, and another...

    And only THEN can you start testing your code to see if it even does what it is supposed to do...

    that is NOT the way to program. It's light attaching 200lb weights to your feet and jumping into the deep end of a pool.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    I have to do it this way. My teacher asked me to do it this way and now I'm stucked
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    You're saying your teacher insisted that you write over 100 lines of code before trying to compile it?
    Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    Something like that. We were not even taught to do it in simpler ways
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11175
        
      16

    I find that very hard to believe...but you're stuck with this now.

    All you can do is fix the first error the compiler reports, and then re-compile. It is generally pointless to try and fix more than one at a time, as an early mistake can mask others, or even make the compiler so confused it reports more than there are.

    Once you have all those errors fixed and you get a .class file, you're going to have to test the heck out of this.
    Azhar Karim
    Greenhorn

    Joined: Feb 13, 2013
    Posts: 10
    I finally found the problem. Thanks for the help guys !
     
     
    subject: Unreachable statement
     
    Similar Threads
    Help with toString message
    need guidance
    assertion
    Target Unreachable
    Code not reachable