Results 1 to 17 of 17
  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default a simple if statement

    hey everyone,
    I've tried this basic piece of javacode.I use bluej to compile and test this.
    The basics are very simple: seconds,minutes and hours that you can fill in yourself. But When you put in a number larger then 60 in the seconds part or smaller then 0,the value should automatically be 0. But when you test this one and for example insert 5000,it doesn't change to 0. I've pasted the code underneath.

    public class Tijd{
    private int sec;
    private int min;
    private int uur;

    public Tijd(int sec, int min, int uur) {
    this.setSec(sec);
    this.setMin(min);
    this.setUur(uur);
    }

    public int getSec() {
    return sec; }
    public int getMin() {
    return min; }
    public int getUur() {
    return uur; }

    public void setSec(int sec) {
    if(this.sec>60) {
    this.setSec(0); }
    if(this.sec<0) {
    this.setSec(0);}
    this.sec=sec;}
    public void setMin(int min) {
    this.min=min; }
    public void setUur(int uur) {
    this.uur=uur; }
    public void reSet() {
    this.uur=0;
    this.min=0;
    this.sec=0; }


    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    Java Code:
    public void setSec(int sec) {
        if(this.sec>60)  {
                this.setSec(0); }
        if(this.sec<0)  {
                this.setSec(0);}
        this.sec=sec;}
    Carefully check what that method does: if sec > 60 or sec < 0 you call that same method again with a value equal to zero (which is correct) but at the end you still set the sec value of your object to the sec parameter, effectively annihilating what those recursive calls had done. Better change that to:

    Java Code:
    if (sec < 0 || sec >= 60)
       setSec(0);
    else
       this.sec= sec;
    kind regards,

    Jos

  3. #3
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    Oops...can't believe I overlooked that :s. Thanks for the reply and sorry for the stupid failure from my side :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    Oops...can't believe I overlooked that :s. Thanks for the reply and sorry for the stupid failure from my side :)
    You're welcome of course and there's no need to apologize; I've done far more stupid things and I still do it on a regular basis ;-)

    kind regards,

    Jos

  5. #5
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    I just noticed that the value is always 0 :s. Adding an else statement saying :

    else(){
    this.setSec(sec); }

    doesn't seem to solve the problem.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    I just noticed that the value is always 0 :s. Adding an else statement saying :

    else(){
    this.setSec(sec); }

    doesn't seem to solve the problem.
    (get rid of those two parentheses) Of course that doesn't solve your problem: suppose sec == 20 so you execute your else statement and call that same method with the same parameter value over and over and over ...

    kind regards,

    Jos

  7. #7
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    then I dont know the answer :). I don't seem to have the right knowledge yet :)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    then I dont know the answer :). I don't seem to have the right knowledge yet :)
    Did you read (and understand) the code snippet I posted?

    kind regards,

    Jos

  9. #9
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    the one with == 20 ? That I don't really understand :)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    the one with == 20 ? That I don't really understand :)
    No, I meant the snippet in reply #2. It's a complete answer to your question.

    kind regards,

    Jos

  11. #11
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    "stackoverflow rror : null" is what I get with the following code:

    public class Tijd{
    private int sec;
    private int min;
    private int uur;

    public Tijd(int sec, int min, int uur) {
    this.setSec(sec);
    this.setMin(min);
    this.setUur(uur);
    }

    public int getSec() {
    return sec; }
    public int getMin() {
    return min; }
    public int getUur() {
    return uur; }

    public void setSec(int sec) {
    if(this.sec>=60) {
    this.setSec(0); }
    if(this.sec<=0) {
    this.setSec(0);}

    }
    public void setMin(int min) {
    if(this.min>=60) {
    this.setMin(0);}
    if(this.min<=0) {
    this.setMin(0); }
    }
    public void setUur(int uur) {
    if(this.uur>=24) {
    this.setUur(0);}
    if(this.uur<=0) {
    this.setUur(0);}
    }
    public void reSet() {
    this.uur=0;
    this.min=0;
    this.sec=0; }


    }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    Java Code:
    public void setSec(int sec) {
        if(this.sec>=60)  {
                this.setSec(0); }
        if(this.sec<=0)  {
                this.setSec(0);}
       }
    Cute; see what happens if you call this method with sec == 0. The method will call itself over and over again. The cure is simple, change '<=' to '<'. But what will happen when you call this method with sec == 20? Please reread my reply #2 again. And why are you checking this.sec instead of the parameter sec?

    kind regards,

    Jos

  13. #13
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    I get this part(I think):

    if (sec < 0 || sec >= 60)
    setSec(0);
    else
    this.sec= sec;

    If the sec value is below 0 or above/equal to 60,the parameter sec wil be set to 0. Otherwise the parameter will get the value inserted right? But that doesn't explain to me why it keeps repeating.

  14. #14
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    it works perfect though :)

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    I get this part(I think):

    if (sec < 0 || sec >= 60)
    setSec(0);
    else
    this.sec= sec;

    If the sec value is below 0 or above/equal to 60,the parameter sec wil be set to 0. Otherwise the parameter will get the value inserted right? But that doesn't explain to me why it keeps repeating.
    My version doesn't repeat; if you want to remove that one step recursion you can even write:

    Java Code:
    if (sec < 0 || sec >= 60)
       this.sec= 0;
    else
       this.sec= sec;
    kind regards,

    Jos

  16. #16
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    oooow I get it now I think. You mean that while I was setting the parameter to 0,it kept changing back to the one that was innitially given? So when it was 500,it changed to 0,then to 500 en then got into a loop?

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    oooow I get it now I think. You mean that while I was setting the parameter to 0,it kept changing back to the one that was innitially given? So when it was 500,it changed to 0,then to 500 en then got into a loop?
    Yup.

    kind regards,

    Jos

Similar Threads

  1. The if Statement, need some help!
    By Keno777 in forum New To Java
    Replies: 6
    Last Post: 10-24-2009, 12:53 AM
  2. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 09:56 PM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

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