leap year code with a do-while loop and multiple methods

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4544 Views - Last Post: 26 February 2012 - 09:26 AM Rate Topic: -----

#1 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

leap year code with a do-while loop and multiple methods

Posted 25 February 2012 - 05:16 PM

hey everyone,

i'm new to this and I have a problem. I have to make my program perform a do-while loop. I have the methods going but I just can get the loops working for me. This is what I have so far:

*Edited: Welcome at DIC :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 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.");
         	}  
        }
   }



if anyone have any suggestions I would greatly appreciate it.

*Edited: code tags added for a newbie

This post has been edited by pbl: 25 February 2012 - 05:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: leap year code with a do-while loop and multiple methods

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: leap year code with a do-while loop and multiple methods

Posted 25 February 2012 - 05:43 PM

I don't see a do while loop in your code. What exactly (be specific) is the issue? Where and what is the loop supposed to do and what have you tried that didn't work?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: leap year code with a do-while loop and multiple methods

Posted 25 February 2012 - 05:49 PM

And what is your problem ?
Was This Post Helpful? 0
  • +
  • -

#4 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 25 February 2012 - 06:33 PM

the problem is that I don't know where exactly to put my do-while loop in the main method area. when i did put it in th main method area, i get an error message. that's the only problem that i'm having with this code.

this is where i put my do-while loop in my 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);
	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.");
         	}  
        }
   }







Was This Post Helpful? 0
  • +
  • -

#5 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 25 February 2012 - 07:11 PM

also this program is to ask the user to enter a year and it then determines if the year is a leap year. once that it done then it asks if the user wants to enter another year. once the user enters "y" the program will continue, but once they enter "n" the program will terminate.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 03:40 AM

The placement of your do/while loop isn't bad, but the logic is a little off. The typical structure of a do/while loop is:
do
{

   // modify the loop control variable

}
while ( loopControl );

If you don't modify the loop control variable somewhere in the loop, then you'll experience an infinite loop.

In your code, you ask the user if s/he wants to continue and assign their response to the variable 'Answer', but the loop control variable used in the while statement is 'repeat,' which isn't declared/defined anywhere, so you should be getting an error like "Can't find symbol."

Also, you have a code block you want to execute if the user wants to continue in an else block, but the else block is a single line. You should use braces, "{}", to include more lines in your else block.
Was This Post Helpful? 0
  • +
  • -

#7 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 03:46 AM

Now I have redesign the loop and the only thing I'm getting is about 4 error messages which is at the do
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);
		}
		
		//Perform as many leaps as the users wants
		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 (repeat == 'y' || repeat == 'Y');
	} 
}



Other than that, that's the only errors I'm getting.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 03:51 AM

Post your error messages.
Was This Post Helpful? 0
  • +
  • -

#9 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 03:52 AM

here are the error messages:
LeapYear.java:39: error: illegal start of type
do
^
LeapYear.java:39: error: ';' expected
do
^
LeapYear.java:93: error: class, interface, or enum expected
while (repeat == 'y' || repeat == 'Y');
^
LeapYear.java:94: error: class, interface, or enum expected
}
^
4 errors
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 04:10 AM

Check your braces in the do/while construction. I think your while is not following the do{}.
Was This Post Helpful? 0
  • +
  • -

#11 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 04:15 AM

i put the while behind the {} and i'm still getting the same errors
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 04:29 AM

I guessed. Your do/while loop is not inside a method.
Was This Post Helpful? 0
  • +
  • -

#13 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 04:33 AM

so I need to put this inside the main method. question because i am very much new at this, where exactly would i put this. i'm more of a networker not a programmer.
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 04:53 AM

Welcome to programming!

Yes, the basic structure of a Java program is classes that contain methods that contain statements. Methods can't appear outside of a class or inside of other methods, and statements can't occur outside of a method. Your class is LeapYear that contains the methods main(), displayInstructions(), getYear(), isLeap(), and displayResults(). However, the last 4 methods are contained inside a do/while loop that is NOT contained in a method. So, you've a bit of a mess to untangle.

The 5 methods should be at the same level inside the class, and the do/while statement will have to be somewhere else, probably inside your main() method. If your code is indented properly, it will look like:

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);
    }

    //Perform as many leaps as the users wants
    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 (repeat == 'y' || repeat == 'Y');
} 
}



Remove the do/while statement, think about where you'd like it to be, post the updated code, and come back with any questions you have about program structure or where the do/while statement should be.
Was This Post Helpful? 0
  • +
  • -

#15 carolinatech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-February 12

Re: leap year code with a do-while loop and multiple methods

Posted 26 February 2012 - 05:27 AM

hey i'm lost at this. i'm about to throw in the towel. this program is getting my nerves. i have moved the do-while loop and i'm still getting the same error messages.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2