0

The program I'm writing is to determine whether or not a year is a leap year. This is an assignment, so I am required to use the four methods I wrote inside the program. The program compiles and runs, it asks for user input at the appropriate places, but it doesn't take the input into the program. Also it's saying that the year is a leap year and looping no matter what has been inputted. I've very confused as to where there error is, since this program seems match the examples we were given.

import java.util.Scanner;

public class LeapYear {
    public static void main(String[] args) {
        boolean repeat;
        String computeanother, yes="yes";
        Scanner kb=new Scanner(System.in);
        int year = -1;
        boolean leap;

        do
        { 
            displayInstructions();
            getYear(year);
            leap = isLeap(year);
            displayResults(year, leap);
            System.out.println("Would you like to compute another year?");
            computeanother = kb.nextLine();

            if(computeanother.equals(yes))
                repeat=true;
            else 
                repeat=false;
        } while(repeat=true);
    }

    public static void displayInstructions()
    {
        System.out.println("This program is designed to predict whether or not a year is a leap year.");
        System.out.println("When prompted please enter a positive number for the year.");
        System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
    }

    public static void getYear(int year)
    {
        Scanner kb = new Scanner(System.in);
        do {
            System.out.println("Please enter the year.");
            year=kb.nextInt();
        } while (year < 0);   
    }

    public static boolean isLeap(int year)
    {
        boolean leap;
        if ((year%4==0 && year%100 != 0) || year%400==0){
            leap = true;
            return true;
        } else {
            leap = false;   
            return false;
        }
    }

    public static void displayResults(int year, boolean leap)
    {
        if (leap = true) {
            System.out.println("The year " +year); 
            System.out.println("is a leap year.");
        } else {
            System.out.println("The year " +year); 
            System.out.println("is not a leap year.");
        }
    }

}

Thanks for everyone's help! The edited code looks like this:

import java.util.Scanner;

public class LeapYear{
public static void main(String[] args){
boolean repeat;
String computeanother, yes="yes";
Scanner kb=new Scanner(System.in);
int year = -1;
boolean leap;
do
{ 
displayInstructions();
getYear(year);
leap = isLeap(year);
displayResults(year, leap);
System.out.println("Would you like to compute another year?");
computeanother = kb.nextLine();
repeat = computeanother.equals(yes);
}while(repeat);
 }  
public static void displayInstructions()
{
System.out.println("This program is designed to predict whether or not a year is a leap year.");
    System.out.println("When prompted please enter a positive number for the year.");
System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
}

public static int getYear(int year)
{
    Scanner kb = new Scanner(System.in);
    do{
        System.out.println("Please enter the year.");
        year=kb.nextInt();
    }while (year < 0);
    return year; 
}

public static boolean isLeap(int year)
{
boolean leap;
year = getYear(year);
if ((year%4==0 && year%100 != 0) || year%400==0){
    leap = true;
    return true;}
else{
    leap = false;   
    return false;}
}

public static int displayResults(int year, boolean leap)
{
year = getYear(year);
if (leap == true){
   System.out.println("The year " +year); 
    System.out.println("is a leap year.");}
else{
   System.out.println("The year " +year); 
    System.out.println("is not a leap year.");}
return year;
}

}
SMoore
  • 23
  • 1
  • 6

3 Answers3

1
while(repeat=true);

In the while loop should be:

while(repeat == true);

or

while(repeat);

Aside from this being noted by everyone it can be noted you make this mistake twice:

 if (leap = true) {

Should be:

if (leap == true) {

or

if (leap) { 
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
  • So the double equals is only necessary in the if condition? When I'm telling the program that leap is true then I only use one equals sign, correct? – SMoore Feb 22 '13 at 16:48
  • == is a comparison while = is used to assign a value. So: int x = 5; and if (x == 12) – Floris Velleman Feb 22 '13 at 17:17
1

You can also shorten your code:

    do{ 
        displayInstructions();
        getYear(year);
        leap = isLeap(year);
        displayResults(year, leap);
        System.out.println("Would you like to compute another year?");
        computeanother = kb.nextLine();
        repeat = computeanother.equals(yes)  //this line makes code shorter 
    } while(repeat);  

Indeed, always avoid code redundancy like this famous pattern:

if(expression) return true; else return false;

That becomes: return expression;

Mik378
  • 21,881
  • 15
  • 82
  • 180
0

Change this:

while(repeat=true);

to

while(repeat==true);

or

while(repeat);

Here while(repeat=true); you are assigning a value, not comparing. while(repeat==true); or while(repeat); this will compare the value. It's always better to test like this while(repeat); instead of obvious while(repeat==true);. I hope it helps.

And you are not getting value for year as -1 because, you are returning from this method getYear(year); but ignoring the value. Change it to:

year = getYear(year);

This should work.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • This does get the program to terminate thank you! But it is still not picking up the user input or testing the number against the leap year conditions. Do you know why that would be? – SMoore Feb 22 '13 at 16:34
  • @SavannahMoore not picking means? can you explain more? – Pradeep Simha Feb 22 '13 at 16:41
  • When I run the program it continues to display the year as the set value of -1, and since -1 is not a leap year it only report the year as not a leap year. – SMoore Feb 22 '13 at 16:45
  • @SavannahMoore, see my edited answer. It should solve your problem – Pradeep Simha Feb 22 '13 at 16:47
  • It runs thank you! The only concern I have left is that when it runs it asks for the year three times. Would you know how to get it to stop doing that? – SMoore Feb 22 '13 at 17:11
  • @SavannahMoore, yes, that's the intended functionality since you are accepting getYear inside do.. while loop :) – Pradeep Simha Feb 22 '13 at 17:13
  • Oh ok, I just wanted to make sure nothing was wrong. Thank you! – SMoore Feb 22 '13 at 17:15