Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: What's Wrong With My Code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default What's Wrong With My Code?

    Please help me, I cannot find the error in the following code. Boolean always returs true in the following program.
    class time
    {   
        int hh, mm, ss;
        public time()
        {
            hh = 0;
            mm = 0;
            ss = 0;
        }
        public time(int h, int m, int s)
        {
            hh = h;
            mm = m;
            ss = s;
        }
        public boolean validate()
        {
            boolean valid = false;       
            if((hh>=0 && hh<=23)||(mm>=0 && mm<=59)||(ss>=0 && ss<=59))
            valid=true;
            return valid;
        }
        public void display()
        {
            if(hh>12) 
            hh=hh-12;
            System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
        }
        public static void main(String args[])    
        {
            time ob = new time(20,55,30);
            if (ob.validate()==true)
            ob.display();
            else
            System.out.println("Invalid format");
        }
    }


  2. #2
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: What's Wrong With My Code?

    Classes need to begin with a Capital letter. So in this case, "time" should be "Time". Change all the mentions of "time" to "Time" and all will be fine. Also I noticed that your validate() method is not exactly accurate. So long as ONLY ONE of the three conditions is satisfied, it will work. You need ALL THREE to be satisfied to be a correct time:

    For your validate() method, replace the "||" with "&&":
        public boolean validate()
        {
            if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
                    return true;
            else
                    return false;
        }

    Following on from that, in the main program, you don't have to say if(ob.validate() == true)...you could just say if(ob.validate()).
    Last edited by TopdeK; May 7th, 2011 at 10:32 AM.

  3. The Following User Says Thank You to TopdeK For This Useful Post:

    arunjib (May 7th, 2011)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    Classes need to begin with a Capital letter
    Not true. The only requirement is that once you name a class, you must stick to that name. It is generally recommended that you start classes with a capital letter, though. The fixed validate logic is correct (since you want to check if all fields are valid, not if one or more fields are valid).

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    TopdeK (May 7th, 2011)

  6. #4
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Thank you very much for helping me. now i have only one problem on my program. If i enter time >23, it should display "Invalid Format". but its is showing the time subtracting 12.
    Here is the final code
    class Time
    {   
        int hh, mm, ss;
        public Time()
        {
            hh = 0;
            mm = 0;
            ss = 0;
        }
        public Time(int h, int m, int s)
        {
            hh = h;
            mm = m;
            ss = s;
        }
    public boolean validate()
        {
            if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
                    return true;
            else
                    return false;
        }
        public void display()
        {
            if(hh>12) 
            hh=hh-12;
            Time ob = new Time(20,55,30);
            if (ob.validate())
            System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
            else
            System.out.println("Invalid format");
        }
     
    }


    Quote Originally Posted by TopdeK View Post
    Classes need to begin with a Capital letter. So in this case, "time" should be "Time". Change all the mentions of "time" to "Time" and all will be fine. Also I noticed that your validate() method is not exactly accurate. So long as ONLY ONE of the three conditions is satisfied, it will work. You need ALL THREE to be satisfied to be a correct time:

    For your validate() method, replace the "||" with "&&":
        public boolean validate()
        {
            if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
                    return true;
            else
                    return false;
        }

    Following on from that, in the main program, you don't have to say if(ob.validate() == true)...you could just say if(ob.validate()).

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    if(hh>12) 
                    hh=hh-12;
            Time ob = new Time(20,55,30);
            if (ob.validate())

    You're automatically subtracting 12 from hh no matter if the format is valid or not. Secondly, you're storing that result back into hh. you should instead either store that result to a temporary variable, move the subtraction to after you've checked that you have a valid time, or remove it all together and have your program display it out in military time (since display() doesn't print out if it's AM or PM anyways).

    Lastly, creating ob is useless since it has no bearing on the current object. Calling validate on that object will always be true since it's always going to be a valid time. Instead, call validate on your current time object.
    Last edited by helloworld922; May 7th, 2011 at 11:39 AM.

  8. #6
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Thank you for pointing out the problems. I would be more greatful if you make the necessary correction on my codes as I am very new in Java programs.
    Quote Originally Posted by helloworld922 View Post
    if(hh>12) 
                    hh=hh-12;
            Time ob = new Time(20,55,30);
            if (ob.validate())

    You're automatically subtracting 12 from hh no matter if the format is valid or not. Secondly, you're storing that result back into hh. you should instead either store that result to a temporary variable, move the subtraction to after you've checked that you have a valid time, or remove it all together and have your program display it out in military time (since display() doesn't print out if it's AM or PM anyways).

    Lastly, creating ob is useless since it has no bearing on the current object. Calling validate on that object will always be true since it's always going to be a valid time. Instead, call validate on your current time object.

  9. #7
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: What's Wrong With My Code?

    Quote Originally Posted by helloworld922 View Post
    Not true. The only requirement is that once you name a class, you must stick to that name. It is generally recommended that you start classes with a capital letter, though. The fixed validate logic is correct (since you want to check if all fields are valid, not if one or more fields are valid).
    Really? My lecturer needs to get his facts right then. Thanks for the clarity
    while(true)
    {
            codeInJava();
    }

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    Yep. The name can start with any letter (upper or lower case), an underscore, or some currency symbols (such as the british pound symbol or dollar symbol). Actually, any valid name for a variable/method is automatically a valid name for a class (and vise-versa). Same goes for interfaces, enums, etc. Of course, these names probably shouldn't be used because they can get extremely confusing.

    class _
    {
        public static void main(String[] $)
        {
            System.out.println($[0]);
        }
    }

    I would be more greatful if you make the necessary correction on my codes as I am very new in Java programs.
    I try not to provide the full solution because I believe you won't learn how to fix your code if I just dump the code. See if you can't take the description I gave and modify your code accordingly.

    To call a member method of the same class, you can leave of all object quantifiers, or use the "this" keyword:

    if(this.validate()) // using the this keyword.
    {
        // this time is valid. do stuff in here
    }
    Last edited by helloworld922; May 7th, 2011 at 12:24 PM.

  11. #9
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: What's Wrong With My Code?

    Awesomesauce!
    while(true)
    {
            codeInJava();
    }

  12. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    Looked up the java language standard, and it turns out that any unicode character which isn't a number or pre-defined java symbol is considered a "JavaLetter", and thus can be used in the name. This is to support writing code in foreign languages, for example Russian, Greek, Korean, etc.

  13. #11
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    i have made my code as follows... now the question is how can i display time as 12 hour format?
    public void display()
        {
    //        if(hh>12) 
    //        hh=hh-12;
    //        Time ob = new Time();
    //        if (ob.validate())
    if(this.validate()) // using the this keyword.
            System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
            else
            System.out.println("Invalid format");
        }
     
    }
    Quote Originally Posted by helloworld922 View Post
    Yep. The name can start with any letter (upper or lower case), an underscore, or some currency symbols (such as the british pound symbol or dollar symbol). Actually, any valid name for a variable/method is automatically a valid name for a class (and vise-versa). Same goes for interfaces, enums, etc. Of course, these names probably shouldn't be used because they can get extremely confusing.

    class _
    {
        public static void main(String[] $)
        {
            System.out.println($[0]);
        }
    }



    I try not to provide the full solution because I believe you won't learn how to fix your code if I just dump the code. See if you can't take the description I gave and modify your code accordingly.

    To call a member method of the same class, you can leave of all object quantifiers, or use the "this" keyword:

    if(this.validate()) // using the this keyword.
    {
        // this time is valid. do stuff in here
    }

  14. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    You can display in a 12 hour format by creating a temporary hour variable, and a temporary string variable to hold "am" or "pm". This variable would hold the value of hh if hh is less than or equal to 12 and the string should contain "am". If hh is greater than 12, the temporary hour variable should hold hh-12 and the string should contain "pm". If hh is 0, the temporary hour variable should hold 12 and the string should contain "am".

    Then, your output code would print out:
    int tempHour = 0;
    String period = null; // either "am" or "pm"
    // TODO: initialize tempHour and period according to my descriptions
    System.out.println("Your time is: "+ tempHour +":"+ mm +":"+ ss + period);

  15. #13
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: What's Wrong With My Code?

    Quote Originally Posted by TopdeK View Post
    Really? My lecturer needs to get his facts right then. Thanks for the clarity
    Someone who knows more than I can correct me, but classes do not need to begin with a capital letter in the sense that is a compile-time or run-time error if they don't. However, it is a coding convention that they do, because it makes all <ClassName> type readily distinguishable from instance variables.

  16. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    Yes, that's correct. That is indeed the recommended convention.

  17. #15
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Sorry! i cannot apply the tecnique in my program. please help me
    Quote Originally Posted by helloworld922 View Post
    You can display in a 12 hour format by creating a temporary hour variable, and a temporary string variable to hold "am" or "pm". This variable would hold the value of hh if hh is less than or equal to 12 and the string should contain "am". If hh is greater than 12, the temporary hour variable should hold hh-12 and the string should contain "pm". If hh is 0, the temporary hour variable should hold 12 and the string should contain "am".

    Then, your output code would print out:
    int tempHour = 0;
    String period = null; // either "am" or "pm"
    // TODO: initialize tempHour and period according to my descriptions
    System.out.println("Your time is: "+ tempHour +":"+ mm +":"+ ss + period);

  18. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    What part are you having trouble with?

    You can perform multiple statements in one if statement by using brackets.

    if(condition== true)
    { // execute all statements in here if condition is true, otherwise do none of them
        System.out.println("doing statement 1");
        System.out.println("doing statement 2");
    }

  19. #17
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    I am feeling shy now!
    I changed my code as follows... but its incorrect. please help me
    public void display()
        {
            if(this.validate()) // using the this keyword.
            {
                if(hh>12)
                {
                hh=hh-12;
            }
            System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
        }
            else
            System.out.println("Invalid format");
        }
    Quote Originally Posted by helloworld922 View Post
    What part are you having trouble with?

    You can perform multiple statements in one if statement by using brackets.

    if(condition== true)
    { // execute all statements in here if condition is true, otherwise do none of them
        System.out.println("doing statement 1");
        System.out.println("doing statement 2");
    }

  20. #18
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's Wrong With My Code?

    Sorry, I'm not trying to intimidate you, but I do want you to think.

    Ok, so first you have your if statement which checks to see if the time is valid:

    if(this.validate())
    {
        // we have a valid time, do stuff in here
    }
    else
    {
        // invalid time
        System.out.println("invalid time");
    }

    Now that you have a valid time, we want to create a local temporary variable we can modify without changing hh of the object. We also want a string to hold if we're in the morning or afternoon. For the sake of simplicity, let's assume it's am.

    int temporaryHH = hh;
    String period = "am"; // this is an assumption

    After assuming it's am, we need to validate that assumption. How do we know if it's am? If temporaryHH is before noon (smaller than 12). Otherwise, it's pm. If it's pm, temporaryHH needs to be decreased by 12.

    if(temporaryHH >= 12)
    {
        // pm
        period = "pm";
        temporaryHH = temporaryHH - 12;
    }

    The last thing to check is if temporaryHH is 0. What does it mean to be 0 am or 0 pm? That's the equivalent of 12 am or 12 pm. To see if you understand this, I'd like you to see if you can figure out how to accomplish this using the above code (remember to operate on temporaryHH, not hh).

    Your final code should look something like this:

    if(this.validate())
    {
        // we have a valid time
        int temporaryHH = hh;
        String period = "am"; // assume we're am
        if(temporaryHH >= 12)
        {
            // actually in pm
            period = "pm";
            temporaryHH = temporaryHH - 12;
        }
     
        // TODO: what code should go here to fix if temporaryHH is 0?
     
        // print out the valid time
        System.out.println("Your time is: "+ temporaryHH +":"+ mm +":"+ ss + period);
    }
    else
    {
        // invalid time
        System.out.println("invalid time");
    }

    If you use brackets like I did (indent in on each open bracket, indent out on each close bracket) you can easily tell if you're missing a bracket, as well as see what code belongs to which condition.

  21. The Following User Says Thank You to helloworld922 For This Useful Post:

    arunjib (May 7th, 2011)

  22. #19
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Thank you Very Much. My problem is now solved.
    Quote Originally Posted by helloworld922 View Post
    Sorry, I'm not trying to intimidate you, but I do want you to think.

    Ok, so first you have your if statement which checks to see if the time is valid:

    if(this.validate())
    {
        // we have a valid time, do stuff in here
    }
    else
    {
        // invalid time
        System.out.println("invalid time");
    }

    Now that you have a valid time, we want to create a local temporary variable we can modify without changing hh of the object. We also want a string to hold if we're in the morning or afternoon. For the sake of simplicity, let's assume it's am.

    int temporaryHH = hh;
    String period = "am"; // this is an assumption

    After assuming it's am, we need to validate that assumption. How do we know if it's am? If temporaryHH is before noon (smaller than 12). Otherwise, it's pm. If it's pm, temporaryHH needs to be decreased by 12.

    if(temporaryHH >= 12)
    {
        // pm
        period = "pm";
        temporaryHH = temporaryHH - 12;
    }

    The last thing to check is if temporaryHH is 0. What does it mean to be 0 am or 0 pm? That's the equivalent of 12 am or 12 pm. To see if you understand this, I'd like you to see if you can figure out how to accomplish this using the above code (remember to operate on temporaryHH, not hh).

    Your final code should look something like this:

    if(this.validate())
    {
        // we have a valid time
        int temporaryHH = hh;
        String period = "am"; // assume we're am
        if(temporaryHH >= 12)
        {
            // actually in pm
            period = "pm";
            temporaryHH = temporaryHH - 12;
        }
     
        // TODO: what code should go here to fix if temporaryHH is 0?
     
        // print out the valid time
        System.out.println("Your time is: "+ temporaryHH +":"+ mm +":"+ ss + period);
    }
    else
    {
        // invalid time
        System.out.println("invalid time");
    }

    If you use brackets like I did (indent in on each open bracket, indent out on each close bracket) you can easily tell if you're missing a bracket, as well as see what code belongs to which condition.

Similar Threads

  1. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM
  2. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  3. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 09:57 AM

Tags for this Thread