Results 1 to 19 of 19
  1. #1
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Need help fixing Java Code

    Hello! I am taking an entry level Java course, and I need to create a Java application that reads an integer value that's input, then determines if the year is a Leap Year or not. (It's a leap year if Divisible by 4, unless it is also divisible by 100 but not 400.. Which is a little confusing to me, but ah well).

    My problem is this.. While trying to compile my code, I get an incompatible types error (using Int when boolean is needed).. But I am not sure what to change in order to get my code to compile! I would appreciate any help.
    This section also has to do with using if statements.

    Code:


    import java.util.Scanner;

    public class Leapyear
    {
    public static void main (String[] args)
    {
    final int dateMade = 1582;
    int year;

    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter a year to see if it is a Leap Year: ");
    year = scan.nextInt();

    if (year < dateMade)
    System.out.println ("Error: Year entered is less than 1582");

    else
    if (year / 4)
    {
    if (year / 100)
    if (year / 400)
    System.out.println (+ year + "is a Leap Year");
    }
    else
    System.out.println (+ year + "is not a Leap year");

    }
    }

    Error:
    C:\Users\N\Desktop\ Collection\Cit 149\Leapyear.java:26:
    incompatible types
    found : int
    required: boolean
    if (year / 4)
    ^

    What can I do to fix this, thanks!

  2. #2
    Melee Summoner
    Join Date
    Dec 2008
    Posts
    36
    BG Level
    1
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Ravencrest

    i dont know to much about jave but it looks like you need to convert the int into a boolean. its not that hard to do. something like this.


    boolean intToBool(int iVal)

    {

    boolean b=true;

    if(iVal==0)b=false;

    return b;
    }

    that might help. not sure though, im not to familiar with java coding.

  3. #3
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Sort out your brackets and indentation. Making it very hard to read and spot the errors the way it is right now.

    The problem is an if statement uses a condition and you've given it a number

    num / 4 is not a condition. It returns a number divided by 4.

    Now if you wanted to say for example...
    if ((num / 4) > 0)
    That would be a condition, it would return a boolean and therefor be compliant.

  4. #4
    E. Body
    Join Date
    Jun 2007
    Posts
    2,351
    BG Level
    7
    FFXIV Character
    Kirby Prime
    FFXIV Server
    Hyperion

    I'm an AS developer and not a Java developer so feel free to ignore this if syntax are different...

    but does year/4 (which I assume is still an int) even work for a conditional statement? The error would seem to indicate that it doesn't and it requires a boolean value.

  5. #5
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Quote Originally Posted by Kirbyprime View Post
    I'm an AS developer and not a Java developer so feel free to ignore this if syntax are different...

    but does year/4 (which I assume is still an int) even work for a conditional statement? The error would seem to indicate that it doesn't and it requires a boolean value.
    No

    EDIT: Not trying to be picky but I just realised this thread is in General.
    Threads like this go in Tech: http://www.bluegartr.com/forum/forumdisplay.php?f=70

  6. #6
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Ah, here's the code again, using - to put indents:
    import java.util.Scanner;

    public class Leapyear
    {
    --------public static void main (String[] args)
    ----------{
    -----------final int dateMade = 1582;
    -----------int year;

    -----------Scanner scan = new Scanner (System.in);

    -----------System.out.println ("Enter a year to see if it is a Leap Year: ");
    -----------year = scan.nextInt();

    -----------if (year < dateMade)
    -----------System.out.println ("Error: Year entered is less than 1582");

    -----------else
    -------------------if (year / 4)
    ----------------------{
    ----------------------if (year / 100)
    ----------------------------if (year / 400)
    -----------------------------System.out.println (+ year + "is a Leap Year");
    ----------------------}
    ----------------------else
    ----------------------System.out.println (+ year + "is not a Leap year");
    ---------}
    }

    Edit:
    "Now if you wanted to say for example...
    if ((num / 4) > 0)
    That would be a condition, it would return a boolean and therefor be compliant. "

    What I would like to say is if the year can be divided by 4, and if it can also be divided by 100 and 400, th it's a leap year.. But I'm not quire sure if that's correct!
    I'm sorry! First time posting Thanks for telling me where it should go

  7. #7
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Quote Originally Posted by Minardi View Post

    Edit:
    "Now if you wanted to say for example...
    if ((num / 4) > 0)
    That would be a condition, it would return a boolean and therefor be compliant. "

    What I would like to say is if the year can be divided by 4, and if it can also be divided by 100 and 400, th it's a leap year.. But I'm not quire sure if that's correct!
    Code:
    if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0)
    {
        is leap year
    }
    % = Modulus. It's like divison except it returns the remainder rather than the division. If x modulus y = 0 then x is cleanly divisible by y.

    && = AND. Joins two conditionals together inside an if or while etc.

    eg. if (x && y). If x = true and y = true, then the statement returns true. If one of them is false or both of them are false, then the statement returns false

    To do it like you had before would just be:
    Code:
    if (year % 4 == 0)
     if (year % 100 == 0)
      if (year % 400 == 0)
       {
           code
       }

  8. #8
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Quote Originally Posted by byte View Post
    if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0)
    {
    is leap year
    }

    % = modulus. It's like divison except it returns the remainder rather than the division. If x modulus y = 0 then x is cleanly divisible by y.

    && = Joins two conditional statements together instead an if or while etc.
    Oh wow, that looks busy. I was trying to see how to use the &&, but apparently I was doing it wrong. Thanks!

    I put in your fix, and now it compiles~. I appreciate the help!

    Question though, why do you have == 0 at the end?

    Edit: Ah, I see- This is just the consolidated statement of all of those put together. Ok!

  9. #9
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    10 % 3 = 1
    Makes 3 parts of 3 with 1 left over

    9 % 3 = 0
    Makes 3 parts of 3 with 0 left over

    14 % 3 = 2
    Makes 4 parts of 3 with 2 left over

    ------------------

    We are testing to see if it has a remainder or not. If there is no remainder (Remainder = 0) then "it divides" as you put it.

    The reason for double == is because we are testing a condition rather than assigning a value.

    For instance..

    int x = 5;
    Using a single =

    if (x == 6)
    Double = because we're checking the value. x still remains 5.

    if (x = 6)
    Would make x contain 6, overwriting the value we already had in it.


    ----

    One more thing...

    Rather annoyingly you have to type out the full statement for every part inside a conditional. For example:

    if (x == 5 && 3 && 4)
    Testing if X is 5 and 3 and 4 (Impossible, but its just for example).

    You must put the values you are testing each time, as if it was a seperate if statement:
    if (x == 5 && x == 3 && x == 4)

    ----

    As well as && there are a few others such as || which means OR.

    x = 1
    y = 0

    if (x == 1 || y == 1)
    IF X OR Y = 1


    The reason for needing 2 &s or 2 |s is complicated so I won't go into it. The reasons are similar to the reason for double ==.

  10. #10
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Oh ok, I wasn't sure what the % symbol was in Java.. I've played a little with remainders when I just had to work with psuedocode (previous class).

    I see what you mean by the == symbol now, thanks for the explanation! Trying to learn this online is.. difficult, haha

    Edit: That makes sense, too bad you can't shorthand it like the first example! Ah, this chapter explained the logical NOT, AND, OR statements, just trying to connect them together is a little difficult at first.. The examples in the book are never quite what I need to see, beh.

  11. #11
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Code:
    Use code tags to preserve indenting on your stuff so it's readable

  12. #12
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Oh yeah and it's kind of useful having the freedom to use the single = inside conditionals, which is the reason for the distinction. You can do cool stuff like this :

    Code:
    x = 5
    if ((x = 12) % 4 == 0)
    x is now 12
    Assignment and a comparison, in one statement. You probably won't end up needing to do this but it might occassionally come in handy.

    Quote Originally Posted by Correction View Post
    Code:
    Use code tags to preserve indenting on your stuff so it's readable
    Thank you ^^. I didn't know this. What a handy feature!

    Quote Originally Posted by Minardi View Post
    The examples in the book are never quite what I need to see, beh.
    There is no perfect programming book. I always suggest reading several so you can gain different perspectives.

  13. #13
    Melee Summoner
    Join Date
    Oct 2006
    Posts
    48
    BG Level
    1

    Quote Originally Posted by Minardi View Post
    Oh ok, I wasn't sure what the % symbol was in Java.. I've played a little with remainders when I just had to work with psuedocode (previous class).

    I see what you mean by the == symbol now, thanks for the explanation! Trying to learn this online is.. difficult, haha

    Edit: That makes sense, too bad you can't shorthand it like the first example! Ah, this chapter explained the logical NOT, AND, OR statements, just trying to connect them together is a little difficult at first.. The examples in the book are never quite what I need to see, beh.
    You could, but then again that's just obfuscating the code for no real apparent reason for such a simple code block.


    Mini-rant (feel free to ignore) ^_^
    Unless you have a reason write something with the minimum amount of lines be as descriptive with variable names as you can, take time to pay attention to layout structure, and coding standards. It will be easier to read, you don't get cool points for making things hard to read if someone else has to review it. I've been doing this shit for 19 years now, I'm really tired of seeing developers trying to condense their code to the point it takes longer to update/maintain that just rewrite it anyways.

    (edit: I feel old now, corrected how long I've been coding professionally for.)

  14. #14
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Thanks Correction, . That's useful to know!

    Oh my, that is both awesome and complicated!

    Thanks a whole bunch for explaining this for me!

    Edit Error: Yeah, I try and indent everything properly and document things, but I was just throwing out the code first to make sure I could get it compile, then go back and pretty that up. I wasn't quite sure how to approach this at first, since the symbols are new (And the examples are short themselves!). I'll keep that in mind though- My friend also complains about how people code sometimes, haha.

  15. #15
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Quote Originally Posted by Minardi View Post
    Thanks Correction, . That's useful to know!

    Oh my, that is both awesome and complicated!

    Thanks a whole bunch for explaining this for me!

    Edit Error: Yeah, I try and indent everything properly and document things, but I was just throwing out the code first to make sure I could get it compile, then go back and pretty that up. I wasn't quite sure how to approach this at first, since the symbols are new (And the examples are short themselves!). I'll keep that in mind though- My friend also complains about how people code sometimes, haha.
    If you're unsure how something works, tinkering around with it is always the best way to find out. For example, I'd make a small program which just tests out one thing, so I understand how it works before I use it in something big. Trying out multiple, completely new concepts at once can be confusing because you might not be able to work out what went wrong!

  16. #16
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    That is a good idea, Byte! I tried commenting out some of my code and testing the && portion I wrote, and it wouldn't compile at that part..
    So I decided to try asking. From the looks of it, I was trying to do that jumbled "all in one" comment example you posted, and without the ==0.

  17. #17
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Quote Originally Posted by Errors View Post
    Unless you have a reason write something with the minimum amount of lines be as descriptive with variable names as you can, take time to pay attention to layout structure, and coding standards. It will be easier to read, you don't get cool points for making things hard to read if someone else has to review it. I've been doing this shit for 19 years now, I'm really tired of seeing developers trying to condense their code to the point it takes longer to update/maintain that just rewrite it anyways.

    ^ This

    I've been working in the field for much less longer of a time (4-ish years) but already run into my fair share of 'wtf' code done in the name of rediculous reasons. Gotta love community college students that abbreviate every variable, no comments, etc. Even some shit I see 'professional' developers do just confuses the hell out of me.

    As a suggestion to any developer... read your code out loud. If it doesn't make any sense out loud, it isn't readable.

  18. #18
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    Quote Originally Posted by Zhais View Post
    ^ This

    I've been working in the field for much less longer of a time (4-ish years) but already run into my fair share of 'wtf' code done in the name of rediculous reasons.
    Code:
    // As long as all calls to this class are made from the same thread 
    //this class is thread safe.
    
    Vec2 GetDesiredPositionOfLastStringSegmentForCarryingObject(Object * pObj)
    {       
      ...
    }

  19. #19
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    I love that comment, lol

Similar Threads

  1. need help with algebra2 :]
    By untouchable in forum General Discussion
    Replies: 43
    Last Post: 2005-12-15, 23:33
  2. Does this sound bogus to anyone? I need help.
    By Octavious in forum General Discussion
    Replies: 14
    Last Post: 2005-08-10, 07:53
  3. Need help on money making methods
    By in forum General Discussion
    Replies: 2
    Last Post: 2004-09-16, 21:57