How do I get my leap year Java program to keep looping?

123 Views Asked by At

I'm trying to finish my final assignment for my intro to programming class and I can't figure out how to keep the program looping until the user inputs 0 to exit. My teacher wanted this exact format, so I'm not allowed to condense the code to make it simpler. We've only been coding with Java for roughly 4 weeks throughout the whole 16-week semester (I don't understand it either) so I'm not very familiar with how to use and understand Java.

Where my program is at currently, is that it takes the input, determines if it's a leap year, outputs the correct string, and then exits. My checkValidYear method outputs the string "Enter 0 if you'd like to exit." but doesn't allow further input. I need some guidance on where/how to loop the program and stop it from ending.

Too sum it up, I need assistance on changing my program from exiting when given an input, to looping the program for more inputs until the user inputs 0.

This is my main file.

/**
 * Main class of the Java program.
 */
import java.util.Scanner;

public class FinalProject {

    public static void main(String[] args) {
        
        
        int usersYear;
        
        //set up the scanner
        Scanner inputReader = new Scanner(System.in);
        
        //Create leap year instance
        LeapYear inputYear = new LeapYear();
      
    do { 
        System.out.println("Enter a year which I will test if it is a leap year. Input 0  to stop");
        
        
        int i = inputReader.nextInt();
        inputYear.setYear(i);
       }
       
         while(inputYear.checkValidYear().equals("invalid"));
        inputReader.close();
        System.out.println(inputYear.getOutput());

       // create a loop to which sees if the user wants to stop
        //   in the loop:
        //     use the setYear() method to set the year inside the object
        //     use the CheckforLeapYear() method to do all the testing. DO NOT SEND YEAR because it was    already set using setYear()
        //     use the getOutput() to get the message you print - is it a leap year or not.
        //     get the next year from the user
            
        
       
        
    } //end main method
} //end Main class 

This is my LeapYear file where the methods are kept.

/**
 LeapYear class determines if a year is a leap year
**/
class LeapYear {
    /**Declarations**/
    /*
    * 1. Create a private year variable that is an integer 
    * 2. Create a getter and setter method for the field variable
    * 3. Create a default constructor.Do not set any variables
    **/
  private int year;
  
  public LeapYear(){
      
  }
  
  // Getter
     public int getYear() {
        return year;
  }

  // Setter
     public void setYear(int newYear) {
        year = newYear;
  }
  
  
    /*
    * This method should call all the checkIfYearDivisibleby4(), checkIfYearNotDivisibleby100(), and checkIfYearDivisibleby400()
    * @return int year
    **/
    public boolean checkForLeapYear(){
        boolean result = false;
        //add code here
      result= checkIfYearDivisibleby4()||
        checkIfYearNotDivisibleby100()||
        checkIfYearDivisibleby400();
      
        return result;
    }
  
    /*
    * This method should return true if year is divisible by 4
    * @return int year
    **/
    private boolean checkIfYearDivisibleby4(){
        boolean result = false;
            //add code here
            result = year%4==0;
            return result;
    }   
    
    /*
    * This method should return true if year is NOT divisible by 100
    * @return int year
    **/
    private boolean checkIfYearNotDivisibleby100(){
        boolean result = false;
        //add code here
        result = year%100==0;
        return result;
            
    }
  
    /*
    * This method should return true if year is divisible by 400
    * @return int year
    **/     
    private boolean checkIfYearDivisibleby400(){
        boolean result = false;
        //add code here
        result = year%400==0;
        return result;

    }
  
    /*
    * This method calls the checkForLeapYear()
    * if the checkForLeapYear() returns true return a string that states it is a leap year
    * otherwise, return a string that states it is not a leap year
    * @return int year
    **/
      
      public String getOutput(){
      String output= "";
      if (checkForLeapYear())
      {
          output = "This is a leap year";
      }
      else
      {
          output = "This is not a leap year";
      }
      return output;
    }
    
    public String checkValidYear(){
        System.out.println("Enter 0 if you'd like to exit.");
        String result = "valid";
        if(year==0)
        result = "invalid";
        return result;
    
        
    }
    
    
}

This is what the output looks like:

Enter a year which I will test if it is a leap year. Input 0 to stop 2023 Enter 0 if you'd like to exit. This is not a leap year

0

There are 0 best solutions below