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;
}
}
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) {
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;
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.
Related
Good afternoon everyone,
I am having an issue calculating multiple leap years from a boolean utility method. I've been told to use a loop, but have a hard time figuring out how and where to put it.
import java.util.Scanner;
public class LeapYear {
private static boolean isLeapYear(int year) {
if (year % 400 == 0 && year % 100 == 0) {
return true;
}
if (year % 100 == 0) {
return false;
}
if (year % 4 == 0) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
int quitter = 0;
int negative = 0;
int year = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a year or -1 to quit: ");
year = scan.nextInt();
while (year != -1 && year < 1582) {
System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
year = scan.nextInt();
}
if(year == -1) {
negative += year;
quitter++;
}
System.out.println(isLeapYear(year));
}
}
Just use a do-while loop:
public static void main(String[] args) {
int year = 0;
Scanner scan = new Scanner(System.in);
do{
System.out.println("Enter a year or -1 to quit: ");
year = scan.nextInt();
while (year < 1582) {
System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
year = scan.nextInt();
}
if(year != -1) {
System.out.println(isLeapYear(year));
}
}while(year!=-1);
}
The last method I was trying to get it to print out my program can't seem to get it to do it. I don't know what to do. And I'm in an online class so there is no one I could really ask. My teacher takes three days to reply.
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
displayInstructions();
int year = getYear();
isLeap(year);
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(boolean year) {
boolean isLeapYear = false;
if (year % 4 == 0 && year != 100) {
isLeap = true;
}
else {
isLeap false;
}
}
public static void displayResuls( boolean isLeap, boolean year) {
if (isLeap)
{
System.out.println("Year" +year+" is a Leap Year.");
}
else {
System.out.println("Year" +year+" is not a Leap Year");
}
}
}
You never call displayResuls(isLeap(year), year).
Edit: also your declaration of displayResults is wrong, it should be int year instead of boolean year.
Your isLeap function should be like:
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
isLeapYear = true;
} else {
isLeapYear = false;
}
return isLeapYear;
}
Main mistake I think in condition. It is more complicated than yours.
Correct one:
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
For example, 2000 - leap, but 2100 - not.
This is your code rewritten. Just like others said, you never called displayResuls and there was some minor typos. Keep coding ;)
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
displayInstructions();
int year = getYear();
boolean ans = isLeap(year);
displayResuls(ans, year);
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if (((year % 4) == 0) && (year != 100)) {
isLeapYear = true;
} else {
isLeapYear = false;
}
return isLeapYear;
}
public static void displayResuls(boolean isLeap, int year) {
if (isLeap) {
System.out.println("Year " + year + " is a Leap Year.");
} else {
System.out.println("Year " + year + " is not a Leap Year");
}
}
}
Made few changes to your program and it works fine
public static void main(String[] args) {
displayInstructions();
int year = getYear();
System.out.println("Entered Year is "+isLeap(year));
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. "
+ "The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if (year%4 == 0 && year != 100) {
return isLeapYear = true;
}
else {
return isLeapYear = false;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
import java.util.Scanner;
public class LeapYearTester{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a year");
int year_input = input.nextInt();
// now make a Year object
Year theYear = new Year(year_input);
// now check to see if it's a leap year
if (theYear.isLeapYear())
System.out.println("That's a leap year!");
else
System.out.println("That's not a leap year!");
}
}
public class Year
{
// declare your instance variables here
private int theYear;
private int Year;
// write your constructor here
public Year(int y)
{
y=Year;
}
static boolean isLeapYear(final int Year) {
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
//(((Year%4==0) || (Year%400)) && ((!Year%100==0) || (Year%400=0)))
}
}
The above is the Main and cannot be changed. Having trouble with class.
Not much modification of your original attempt other than adding a check to see if the user entered a valid year in this case between 1000 and 2999 inclusive using regex (I will leave it as a task for you to modify this code for what you consider as a valid year):
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LeapYearTester {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a year: ");
String yearInput = "";
String pattern = "^[12][0-9]{3}$";
Pattern r = Pattern.compile(pattern);
//checking is valid year
while(input.hasNext()){
yearInput = input.nextLine();
Matcher m = r.matcher(yearInput);
if(m.find()) {
System.out.println("You entered the valid year: " + m.group(0));
break;
} else {
System.out.println("Error: Not a valid year");
System.out.print("Please enter a year between 1000 and 2999 inclusive: ");
}
}
Year theYear = new Year(Integer.valueOf(yearInput));
if(theYear.isLeapYear()) {
System.out.println("That's a leap year!");
} else {
System.out.println("That's not a leap year!");
}
}
public static class Year {
private int year;
private Year(int y) {
year = y;
}
private boolean isLeapYear() {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
}
}
Example usage: Testing a leap year
Please enter a year: 2016
You entered the valid year: 2016
That's a leap year!
Example usage: Invalid input and testing a non leap year
Please enter a year: 3000
Error: Not a valid year
Please enter a year between 1000 and 2999 inclusive: 2015
You entered the valid year: 2015
That's not a leap year!
Try it here!
Error: Duplicate local variable yearb
I been asked to put the month, year , day 3 separate variables.
You will send these values to a convertdays method that will return the date in the form of one long integer using the following formula: your long year will be the sum of the year multiplied by 10000, the month multiplied by 100, and the day. This is a formula used to put the date in a form that can be sorted (we will not be sorting dates, just putting it into a form that can be sorted). Back in main you will print this labeled long integer.
/* rayane seridj
* 5-10
* */
import java.util.Scanner;
public class hw2222
{
public static void main (String [] args)
{
int month, day, year,dates,yearb;
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Date in The Format mm/dd/yyyy : ");
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();
boolean yearb;
yearb = leap(year);
if (yearb == true)
System.out.println (year + " is a leap year ");
else
System.out.println (year + " is not a leap year");
}
public static int convertdays (int month ,int day, int year)
{
int yy,dd,mm,dates;
yy =10000 *year;
mm = 100 *month;
dd= day;
dates =yy + mm +dd;
return dates;
}
public static boolean leap(int year)
{
if (year % 400 == 0 || (year % 4 ==0 && year %100 !=0))
return true;
else
return false;
}
}
You declared variable yearb twice:
at int month, day, year,dates,yearb;
at boolean yearb;
A variable can only be declared once in a particular context (method, global, etc).
Try the code below (note a new variable named isLeap):
/* rayane seridj
* 5-10
* */
import java.util.Scanner;
public class hw2222
{
public static void main (String [] args)
{
int month, day, year,dates,yearb;
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Date in The Format mm/dd/yyyy : ");
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();
boolean isLeap;
isLeap = leap(year);
if (isLeap)
System.out.println (year + " is a leap year ");
else
System.out.println (year + " is not a leap year");
}
public static int convertdays (int month ,int day, int year)
{
int yy,dd,mm,dates;
yy =10000 *year;
mm = 100 *month;
dd= day;
dates =yy + mm +dd;
return dates;
}
public static boolean leap(int year)
{
if (year % 400 == 0 || (year % 4 ==0 && year %100 !=0))
return true;
else
return false;
}
}
I have been trying to exit the loop by using various methods, This is the closest I've gotten but still it has a Scanner mismatch error.
Could someone point out what im missing?
Code:
import java.util.*;
public class LeapYear
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean four ;
boolean hundred;
boolean four_hundred;
boolean quit=true;
int check= 2000;
int min=1582;
String test;
//System.out.println("Enter a year to check if it is a leap year");
do {
do {
System.out.println("Enter a year to check if it is a leap year");
if(check <min ) {
System.out.println("That year is too old, choose a year in more recent history");
}
check =input.nextInt();
} while( check < min);
// check =input.nextInt();
if(check % 4==0) {
four=true;
} else {
four=false;
}
if(check%100==0) {
hundred=true;
} else {
hundred=false;
}
if(check %400==0) {
four_hundred=true;
} else {
four_hundred=false;
}
if(four==true) {
if(four==true&&hundred==false&&four_hundred==false) {
System.out.println("The year is a leap year");
}
if(four==true&&hundred==true&&four_hundred==false) {
System.out.println("The year is not a leap year");
}
if(four==true&&hundred==true&&four_hundred==true) {
System.out.println("The year is a leap year");
}
} else {
System.out.println("The year is not a leap year");
}
System.out.println("DO YOU WANT TO QUIT: Y/N ?");
while(!input.hasNext("true|false")) {
System.out.println("That is neither a true or false.");
input.nextBoolean();
if(input.hasNextBoolean()) {
quit=true;
}
if(input.hasNextBoolean()) {
quit=false;
}
}
} while(!quit==false);
}
}
You could make the outer loop a while true loop. Then, when you want to quit, you just return from the method, therefore stopping the method & loop. No need for the quit sentinel value
You also prompt for Y/N, so you should check for that, not true or false.
String q = "n";
do {
System.out.println("Do you want to quit: Y/N ?");
String q = input.next();
if (q.equalsIgnoreCase("y")) return; // quit right here
else if (!q.equalsIgnoreCase("n"))
{
System.out.println("That is neither a y or n.");
continue; // repeat
} else { } // entered y, so continue on with the outer loop
} while (!q.equalsIgnoreCase("y") || !q.equalsIgnoreCase("n"));
And general tip. Writing this pattern is a very beginner thing to do, and it may look nice and easy to understand
if(check %400==0) {
four_hundred=true;
} else {
four_hundred=false;
}
But, you really should just write like so
four_hundred = check % 400;
and here, why get four is true inside the condition where you are guaranteed its value?
if(four==true) {
if(four==true&&hundred==false&&four_hundred==false) {
System.out.println("The year is a leap year");
}
if(four==true&&...
There are a few issues in your code that would explain why you cannot exit the loop.
System.out.println("DO YOU WANT TO QUIT: Y/N ?");
while(!input.hasNext("true|false")) {
System.out.println("That is neither a true or false.");
input.nextBoolean();
if(input.hasNextBoolean()) {
quit=true;
}
if(input.hasNextBoolean()) {
quit=false;
}
}
With input.nextBoolean(); you read the input but there are two issues:
an InputMismatchException will be thrown if the input cannot be scanned into true or false
you consume the input but do not put it into a variable
Ignoring the first issue for the moment, the problem is that then try to set quit based on another check of the input. This means that input.hasNextBoolean() can only be true if the user enters true or false one more time.
Aside from this while loop, there is also a problem with the outer most do/while
...
} while(!quit==false);
You are testing not quit and stay in the loop if it is false. This means that when quit == true you stay in the loop.
You probably want to write while (!quit).
I would suggest to improve your code:
Improve a names of variables.
Remove unused variables.
Simplify if statement expressions.
Here is a code:
import java.util.*;
public class LeapYear
{
public static void main( String[] args ) {
Scanner input = new Scanner(System.in);
boolean isFour;
boolean isQuit = true;
int checkedYear = 2000;
int minimumAcceptedYear = 1582;
//System.out.println("Enter a year to check if it is a leap year");
do {
do {
System.out.println("Enter a year to check if it is a leap year");
checkedYear = input.nextInt();
if ( checkedYear < minimumAcceptedYear ) {
System.out.println("That year is too old, choose a year in more recent history");
}
} while ( checkedYear < minimumAcceptedYear );
isFour = (checkedYear % 4) == 0;
if ( isFour ) {
System.out.println("The year is a leap year");
} else {
System.out.println("The year is not a leap year");
}
System.out.println("DO YOU WANT TO QUIT: (true/false) ?");
if ( input.hasNextBoolean() ) {
isQuit = input.nextBoolean();
} else {
System.out.println("That is neither a true or false.");
}
} while ( !isQuit );
}
}
This is looks a far better. I would you suggest extract
isFour = (checkedYear % 4) == 0;
if ( isFour ) {
System.out.println("The year is a leap year");
} else {
System.out.println("The year is not a leap year");
}
to another method isLeapYear with parameter aYear. You has a mismatch error because of you was giving invalid input data after checking is given year too old. You should move if condition below checkedYear assigment to avoid these error. I hope that's helpful answer.