Results 1 to 5 of 5
  1. #1
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default for loop leap years

    I'm supposed to write a program where the user inputs 2 years and use a for loop to test each year in that range if its a leap year and then output that into a string if its a leap year.


    I have all of the input that the user puts in but i'm not really sure how i would put in the for loop, because i haven't used for loops before and not sure how i would go about doing that.

    I don't have that much in the code so far but i guess i'll print it out here

    int input = 0;
    int input2 = 0;
    int year=0;
    String inputValue = JOptionPane.showInputDialog(null, "Enter start year");
    String inputValue2 = JOptionPane.showInputDialog(null, "Enter final year");




    input = Integer.parseInt(inputValue);
    input2 = Integer.parseInt(inputValue2);
    System.out.println(input);

    for(int i =0; input <= i && i <= input2; ){ ?????

    pretty sure what i have up there is wrong atm
    Last edited by tabako; 10-13-2010 at 04:10 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you can do is, store them in an array.

    Java Code:
    String inputs = new String[2];
    
    inputs[0] = input;
    inputs[1] = input2;
    Then loop withing the array.

  3. #3
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default

    it looks simple

    Java Code:
    List leapYears = new ArrayList();
    		
    for(int i=input;i<=input2;i++){
        if(isLeapYear(i)){
            leapYears.add(i);
        }
    }
    
    System.out.println("Leap Years:");
    for(int i=0;i<leapYears.size();i++){
        System.out.println(leapYears.get(i));
    }
    
    public boolean isLeapYear(int n){
      //leap year logic should go here.
    }
    Last edited by Eranga; 10-13-2010 at 10:54 AM. Reason: code tags added

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please use code tags next time. ;)

    By the time why don't you try to explain your code to our OP.

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    For computing leap years, I love to read Roedy's excellent discusssion here:

    leap year : Java Glossary

Similar Threads

  1. How many years have you been using Java?
    By derrickD in forum Jobs Discussion
    Replies: 41
    Last Post: 05-06-2011, 07:15 AM
  2. Leap Year Calculator
    By Pkaay in forum New To Java
    Replies: 9
    Last Post: 10-12-2010, 10:47 PM
  3. Java is 15 years old - what is the Future?
    By mikeVB in forum Jobs Discussion
    Replies: 1
    Last Post: 03-04-2010, 04:21 AM
  4. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 08:50 PM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 03:46 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
  •