Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable statement

 
Azhar Karim
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program has an error which says unreachable error.

total0=principle0+calcInterest;
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway can I attach photos here?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Azhar Karim
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this fine?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What am I supposed to do now? I need to send this by tomorrow to my teacher
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48363
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have lots of time to implement improvements.
     
    Azhar Karim
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4565
    8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the error is not only for interest1. All the other inputs and results will have errors saying it's not initialized
     
    Junilu Lacar
    Bartender
    Pie
    Posts: 7257
    44
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12083
    29
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Azhar Karim
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have to do it this way. My teacher asked me to do it this way and now I'm stucked
     
    Paul Clapham
    Sheriff
    Posts: 20711
    29
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're saying your teacher insisted that you write over 100 lines of code before trying to compile it?
     
    Azhar Karim
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Something like that. We were not even taught to do it in simpler ways
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12083
    29
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I finally found the problem. Thanks for the help guys !
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic