hey everyone,

I'm trying to perform a program that will perform a leap year method and also have it to loop. here's what i have so far:

import java.util.Scanner;

public class LeapYear
{
   //Create a Scanner object for keyboard input.
   static Scanner keyboard = new Scanner(System.in);

   //Set variables
   public static void main(String[] args)
   {
     int year;
     displayInstructions();     year = getYear();
     displayResults(isLeap(year), year);

      //Have the user to enter yes or no to get another year.   
      char Answer;  
      System.out.println(); 
      System.out.print("Would you like to enter another year? y/n: ");
      Answer = keyboard.next().charAt(0);

      if (Answer =='n')
      {  
    System.out.println("Thank you for entering a year!");    
    System.out.println();
      }
      else
        year = getYear(); 
        displayResults(isLeap(year), year);
        }

        //Display the instructions for the program.
            public static void displayInstructions()
        {   
                System.out.println("This program allows the user to enter a year then,");
                System.out.println("the program determines whether or not the year entered");
                System.out.println("is a leap year.");
                System.out.println();
        }

            //Get the year from the user
        public static int getYear()
        {
                int Year;
                System.out.print("Please enter a year of your choice: ");
                Year = keyboard.nextInt();
                return Year;    
        }

        //Determine if the year entered is leap year or not
        public static boolean isLeap(int Year)
        {   
            if (Year % 4 != 0)
            {
                    return false;
            }
            else if (Year % 400 == 0)
            {
                    return true;
            }
            if (Year % 100 == 0)
            {
                    return false;
            }       
            else
            {
                    return true;
            }
            }

            //Display the year as being either a leap year or not.
        public static void displayResults(boolean LeapYear, int year)
        {
                if (isLeap(year))
            {
                System.out.println("  " + year + " is a leap year.");
            }
            else 
            {
                System.out.println("  " + year + " is not a leap year.");
            }  
        }
   }

This code is to repeat until the user enter the letter n, which will terminate the program. I'm stuck at this point. Could anyone help.

what exactly do u want help with? did you run your code and is it working?
You have redundant if statements; you should combine the 2 true statements and 2 false statements

"code is to repeat " tells you you need a loop. You can put the code enter a year and display the result into a while loop, and keep looping until the user enters "n".

what i'm having a problem with is where i need to put my do-while loop for this program. this code is to ask the user if they want to enter another year. once they enter the letter "n" the program terminates, but as long as they keep entering "y" the program continues. Any help would be greatly appreciated.

Put the loop around the code that asks the user for input and gets his responses.

Try it a few ways and change it if it doesn't do what you want. Do some experimenting to see where the best place is to put it.

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

Here's what I did so far:

import java.util.Scanner;
	
public class LeapYear
{
	//Create a Scanner object for keyboard input.
   static Scanner keyboard = new Scanner(System.in);
   
   //Set variables
   public static void main(String[] args)
   {
     int year;
     displayInstructions();     year = getYear();
     displayResults(isLeap(year), year);
	do
	{
	  //Have the user to enter yes or no to get another year.	
      char Answer;	
    	System.out.println();	
      System.out.print("Would you like to enter another year? y/n: ");
		Answer = keyboard.next().charAt(0);

	if (Answer =='n')
      	{  
		System.out.println("Thank you for entering a year!");    
   		System.out.println();
     	}
      	else
      	year = getYear(); 
       	displayResults(isLeap(year), year);
	}while (repeat == 'n');
		
   		//Display the instructions for the program.
		public static void displayInstructions()
   	{	
   		System.out.println("This program allows the user to enter a year then,");
     		System.out.println("the program determines whether or not the year entered");
     		System.out.println("is a leap year.");
     		System.out.println();
   	}
   		
		//Get the year from the user
   		public static int getYear()
   		{
    			int Year;
    			System.out.print("Please enter a year of your choice: ");
    			Year = keyboard.nextInt();
    			return Year;	
   		}
   
   		//Determine if the year entered is leap year or not
   		public static boolean isLeap(int Year)
   		{	
   			if (Year % 4 != 0)
   			{
        			return false;
      		}
      		else if (Year % 400 == 0)
      		{
        			return true;
      		}
      		if (Year % 100 == 0)
      		{
        			return false;
      		}		
      		else
      		{
        			return true;
      		}
  			}
   
    		//Display the year as being either a leap year or not.
      	public static void displayResults(boolean LeapYear, int year)
      	{
        	if (isLeap(year))
         	{
            	     System.out.println("  " + year + " is a leap year.");
         	}
         	else 
         	{
            	     System.out.println("  " + year + " is not a leap year.");
         	}  
        }
   }

Everything I try to run it, I get 21 error messages that displayInstructions have an error. I don't see where it could but, I'm new at this and need some help figuring out how it could be. When I take the do-while loop out the program runs just fine, but only allows the user to enter a year and ask only one time and that's it.

You can't define a method inside another method - that's why the line in red is an error. It's the calls to those methods that need to be in the loop, not the method definitions.

just replace "repeat" to "Answer" in while condition of do-while loop and declare "Answer" outside the do-while loop.

i changed how the code is and now i'm only getting 4 error messages. here's the code

import java.util.Scanner;
	
public class LeapYear
{
	//Create a Scanner object for keyboard input.
   static Scanner keyboard = new Scanner(System.in);
   
   //Set variables
   public static void main(String[] args)
   {
     int year;
     displayInstructions();     year = getYear();
     displayResults(isLeap(year), year);
	  
	  //Have the user to enter yes or no to get another year.
	  	char repeat;	
      char Answer;	
    	System.out.println();	
      System.out.print("Would you like to enter another year? y/n: ");
		Answer = keyboard.next().charAt(0);

		if (Answer =='n' || Answer == 'N')
      {  
			System.out.println("Thank you for entering a year!");    
   		System.out.println();
     	}
      	else
      	year = getYear(); 
       	displayResults(isLeap(year), year);
		}
	do
	{	
		//Display the instructions for the program.
		public static void displayInstructions()
 		{	
  		System.out.println("This program allows the user to enter a year then,");
   	System.out.println("the program determines whether or not the year entered");
 		System.out.println("is a leap year.");
 		System.out.println();
  		}
   		
		//Get the year from the user
 		public static int getYear()
 		{
 			int Year;
  			System.out.print("Please enter a year of your choice: ");
  			Year = keyboard.nextInt();
  			return Year;	
  		}
   
		//Determine if the year entered is leap year or not
 		public static boolean isLeap(int Year)
 		{	
 			if (Year % 4 != 0)
  			{
  				return false;
   		}
   		else if (Year % 400 == 0)
   		{
   			return true;
   		}
    		if (Year % 100 == 0)
     		{
     			return false;
     		}		
     		else
     		{
     			return true;
     		}
  		}
		//Display the year as being either a leap year or not.
		public static void displayResults(boolean LeapYear, int year)
  		{
     		if (isLeap(year))
     		{
  	    	 System.out.println("  " + year + " is a leap year.");
     		}
     		else 
     		{
  	    	 System.out.println("  " + year + " is not a leap year.");
     		}  
   	}
	}while (answer == 'y' || answer == 'Y');
}

and the error messages are:

LeapYear.java:38: error: illegal start of type
do
^
LeapYear.java:38: error: ';' expected
do
^
LeapYear.java:90: error: class, interface, or enum expected
}while (answer == 'y' || answer == 'Y');
^
LeapYear.java:91: error: class, interface, or enum expected
}
^

Edit: just noticed that the do while should be in the method

get your self eclipse IDE or intelij and save yourself a lot of stress.

The code still has a method being defined inside of another method.
See James's post a from a couple of hours ago.

get your self eclipse IDE or intelij and save yourself a lot of stress.

That's one person's opinion. The majority view from the more experienced contributors here is that a full-scale industrial IDE like Eclipse hinders rather than helps a beginner to get a good grounding in Java. Auto correcting and completing code is only useful once you understand what is being auto corrected or completed and why. The IDE itself also has a huge learning curve, which is not a good thing to add to the Java language and API learning curves. Most of us recommend a programmer's editor and command line JDK for the early stages of learning, or maybe a very simple IDE. But, once again, it's all a matter of opinion.
J (Eclipse user)

ok but where should i put the do-while loop. that's the problem i'm having. when i put it in the main i get error messages

could you post your current code so we may see what you did

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.