Results 1 to 7 of 7
  1. #1
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default Unreachable Statement (accessors) - works as a main method!

    I've got this to turn the length of a music track into seconds, it works fine as a main methods (as below):



    However, I'm putting it in my accessor method like this, and I'm getting an "Unreachable Statement" on the variable trackLengthToSecs.


    Last edited by thomase; 03-12-2009 at 12:50 PM. Reason: removing private code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please repost the entire class reformatted. Good luck.

  3. #3
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please repost the entire class reformatted. Good luck.
    Here you go...there are a few more things there such as String trackName; and String trackLenght; which are different.


    Last edited by thomase; 03-12-2009 at 12:50 PM. Reason: removing private code

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What in the trackDetails() method will be called after this statement has been reached?
    Java Code:
    return trackName + "|" + trackLenght;

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Is there a reason you use the name trackLenght, spelled incorrectly, while you use trackLengthToSecs, spelled correctly, or you're just too lazy to change it? ;)

    Your error is here
    Java Code:
    return trackName + "|" + trackLenght;
    
    
    //Gets total minutes
            int secInt1 = (int) trackLengthToSecs;
    // Etc.
    After you've returned (a value), it stops with that method, so you can't put anything after that.

    Btw.. why don't you just use this?
    Java Code:
    public int toSeconds(final double time) {
        return (int)(time)*60+(int)((time-mins)*100);
    }
    I die a little on the inside...
    Every time I get shot.

  6. #6
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Supamagier, the amount of times i've spelt leng, lenth, length!!, wrong during this one little bit of java coding is amazing :P I now don't care how it's spelt, as long as it works :p.

    Thanks for pointing out the above return is stopping it from working - I should of really realised that, I just thought it'd be more complicated :P.

    Quote Originally Posted by Supamagier View Post
    After you've returned (a value), it stops with that method, so you can't put anything after that.

    Btw.. why don't you just use this?
    Java Code:
    public int toSeconds(final double time) {
        return (int)(time)*60+(int)((time-mins)*100);
    }
    That would possibly make more sense :P. The reason it was longer was because it took me a little while to work out how I wanted to do it so did it in a few stages - think I'll leave it like that for now (cheers for the better method though - made me actually think about shrinking stuff down after I've done it!!).

    Also, I might copy the public int toSeconds(final double time) { bit. The question mentions using a toString method - is what you've got a to string, or am I reading too much into the to in front of seconds :P

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by thomase View Post
    Supamagier, the amount of times i've spelt leng, lenth, length!!, wrong during this one little bit of java coding is amazing :P I now don't care how it's spelt, as long as it works :p.
    It's spelled length ;)
    Quote Originally Posted by thomase View Post
    Thanks for pointing out the above return is stopping it from working - I should of really realised that, I just thought it'd be more complicated :P.
    Yea, you should ^^ beginner mistake. xD
    Quote Originally Posted by thomase View Post
    That would possibly make more sense :P. The reason it was longer was because it took me a little while to work out how I wanted to do it so did it in a few stages - think I'll leave it like that for now (cheers for the better method though - made me actually think about shrinking stuff down after I've done it!!).
    You're welcome m8
    Quote Originally Posted by thomase View Post
    Also, I might copy the public int toSeconds(final double time) { bit. The question mentions using a toString method - is what you've got a to string, or am I reading too much into the to in front of seconds :P
    Go ahead.
    To return a String, just change public int into public String and change return blabla to return String.valueOf(blabla). :)
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Replies: 9
    Last Post: 11-04-2011, 03:09 AM
  2. Using accessors properly
    By LifeWithJava in forum New To Java
    Replies: 2
    Last Post: 12-23-2008, 02:49 PM
  3. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM
  4. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07:40 PM
  5. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 03:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •