I am basically refining, completing and trying to compile my code for an exercise. The objective is to create a calendar.
Unable to figure the problem, have tried to find the inappropriate statements but no solution. I have tried to find other sources in order to gain a solution but I could not figure out the problem.
The error:
MyCalendar.java:60: illegal start of expression
else if(year%4!==0)
^
The code:
public class MyCalender {
int day, month, year;
boolean isDateValid = true;
public MyCalender(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
if (month > 12) //Day and Month validation
{
isDateValid = false;
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 12) {
if (day <= 31) {
isDateValid = true;
} else if (day >= 31) {
isDateValid = false;
}
} else if (month == 2 || month == 4 || month == 6 || month == 8 || month = 10 || month == 12) {
if (day <= 30) {
isDateValid = true;
} else if (day >= 30) {
isDateValid = false;
}
} else if (month == 2) //Consideration of February month and leap year validation
{
if (year % 4 == 0) {
if (day <= 29) {
isDateValid = true;
} else if (day >= 29) {
isDateValid = false;
}
} else if (year % 4 != = 0) {
if (day <= 28) {
isDateValid = true;
} else if (day >= 28) {
isDateValid = false;
}
}
}
}
boolean isDateValid() {
if (isDateValid) {
System.out.println("is a Valid Date");
return true;
}
if (!isDateValid) {
System.out.println("is not a Valid Date,please re-input Date");
return false;
}
return isDateValid;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public static void main(String[] args) {
MyCalender d = new MyCalender(29, 02, 2019);
System.out.println("Date" + d.getDay() + "/" + d.getMonth() + "/" + d.getYear());
d.isDateValid();
MyCalender d1 = new MyCalender(25, 02, 2019);
System.out.println("Date" + d1.getDay() + "/" + d1.getMonth() + "/" + d1.getYear());
d1.isDateValid();
}
}
The output is expected to be:
java MyCalendar 29/02/2019
29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019
25/05/2019 is a Saturday and located in the fourth week of May 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
You have to use != rather than !==.
Adding parentheses helps too.
else if( (year%4) != 0 )
…instead of:
else if(year%4 !== 0)
Related
I'm trying to make it so the user has 3 chances to enter correct input, and after the 3rd try the program should close and I'll give a custom String. If at any point they enter valid data the program should execute. I would like to add error handling to the code:
import java.util.Scanner;
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int month, day; String season = null;
System.out.print("Enter Month & Day: ");
month = in.nextInt(); day = in.nextInt();
if(1 <= month && month <= 3){
season = "Winter";
if((month == 3) && (21 <= day))
season = "Spring";
} else if (4 <= month && month <=6){
season = "Spring";
if((month == 6) && (21 <= day))
season = "Summer";
} else if (7 <=month && month <=9){
season = "Summer";
if((month == 9) && (21 <= day))
season = "Fall";
} else if (10 <= month && month <= 12){
season = "Fall";
if((month == 12) && (21 <= day))
season = "Winter";
}
System.out.println(season);
}
}
Something like this might help you. Code is self explanatory.
private static final int RETRY_COUNT = 3
retryCount = 0
boolean invalid = true;
while(invalid && retryCount++ < RETRY_COUNT){
try{
invalid = false;
Scanner sc = new Scanner(System.in)
.
.
.
}catch(Exception e){
invalid = true;
}
}
I'm supposed to make code that shows the next day, and this works except for the months that end in 31. For example, when I enter 3/31/2000, it gives me 4/2/2000 and skips the first day? I'm not sure why?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab53;
import java.util.Scanner;
/**
*
* #author Owner
*/
public class Lab53 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner keyboardInput = new Scanner(System.in);
int year, month, day;
System.out.println("Enter year/month/day");
year = keyboardInput.nextInt();
month = keyboardInput.nextInt();
day = keyboardInput.nextInt();
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
{
day=thirtyOneDaysMonth(day);
if(day==1 && month==12){
++year;
month=1;
}
else if(day==1 && month!=12)
++month;
}
if ((month==2 || month == 4 || month == 6 || month == 9 || month == 11))
{
day=thirtyDaysMonth(day);
if(month==2 && isLeapYear(year))
{
if(day>29)
{
++month;
day=1;
}
}
else if( day>28 && month==2)
{
++month;
day=1;
}
else
{
if(day==1)
{
++month;
day=1;
}
}
}
System.out.println("The next date is:"+ month + "/" + day + "/" + year);
}
public static int thirtyOneDaysMonth(int day)
{
if(day==31)
day=1;
else
++day;
return day;
}
public static int thirtyDaysMonth(int day)
{
if(day==30)
day=1;
else
++day;
return day;
}
public static boolean isLeapYear(int year)
{
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
else
return false;
}
}
Change if ((month==2 || month == 4 || month == 6 || month == 9 || month == 11)) to else if ((month==2 || month == 4 || month == 6 || month == 9 || month == 11)).
On a day = 31 you're updating the month in your if statement. Then you leave the if but then enter your next if statement that handles months with 30 days. Your code then increments the day again.
Because after the first if has increased 3/31/2000 to 4/1/2000 the second if is evaluated which will again increase the day. Make the second if an else if instead.
In the case of month==3 and day==31, day is bumped back to 1 by
day=thirtyOneDaysMonth(day);
then the month is bumped up by
else if(day==1 && month!=12)
++month;
then the next if statement is true because now month == 4
if ((month==2 || month == 4 || month == 6 || month == 9 || month == 11))
connecting it to the previous if statement with an else if will fix the issue
else if ((month==2 || month == 4 || month == 6 || month == 9 || month == 11))
PUBLIC CLASS
public class MonthDays {
private int month;
private int year;
public int Month(int x){
month = x;
return x;
}
public int Year(int y){
year = y;
return y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 || //determines if the the month entered is a month with 31 days or not
month == 7 || month == 8 ||month == 10 || month == 12){
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
DRIVER CLASS
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
Month month = new Month(reader.nextInt());
System.out.println("Enter year: ");
Year year = new Year(reader.nextLine());
}
}
im not too sure where i went wrong, but from my understanding this should be working. but im also pretty certain its a dumb error somewhere.
The class you're testing is MonthDays. You have methods Month and Year (which should probably be called setMonth and setYear).
MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.Month(reader.nextInt());
System.out.println("Enter year: ");
md.Year(reader.nextInt());
or, if you rename the methods,
MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.setMonth(reader.nextInt());
System.out.println("Enter year: ");
md.setYear(reader.nextInt());
or, add a constructor to MonthDays that takes month and year.
public MonthDays(int month, int year) {
this.month = month;
this.year = year;
}
and call it like
System.out.println("Enter month: ");
int month = reader.nextInt();
System.out.println("Enter year: ");
int year = reader.nextInt();
MonthDays md = new MonthDays(month, year);
Finally, you can call getNumberOfDays once you have a MonthDays instance. Like
System.out.println(md.getNumberOfDays());
This should get your code working. Please read about constructors in Object-Oriented Programming.
public class
public class MonthDays {
private int month;
private int year;
public MonthDays(int x, int y){
month = x;
year = y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 month == 7 || month == 8 ||month == 10 || month == 12){ //determines if the the month entered is a month with 31 days or not
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
driver class
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
int days;
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
x = reader.nextInt();
System.out.println("Enter year: ");
y = reader.nextInt();
MonthDays md = new MonthDays(x, y);
days = md.getNumberOfDays();
System.out.println("Number of days= " + days);
}
}
Hai You have made some basic mistakes in your code .
1) Month is a method ,not a class.You should have needed an instance of MonthDays to call method Month .
As per java coding standard you should have written that as setMonth() or setYear()
2) Method Month return an int value.You should have assigned that to int value.
3) Argument of Year method also a int value.You have specified that a String value now
public class MonthDays
{
private int month;
private int year;
public int setMonth(int x){
month = x;
return x;
}
public int setYear(int y){
year = y;
return y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 || //determines if the the month entered is a month with 31 days or not
month == 7 || month == 8 ||month == 10 || month == 12){
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
MonthDays MD=new MonthDays();
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
int month = MD.setMonth(reader.nextInt());
System.out.println("Enter year: ");
int year = MD.setYear(reader.nextInt());
int numberOfDays=MD.getNumberOfDays();
System.out.println("Number oF days in that month :"+numberOfDays);
}
}
I would like to know what are my mistakes in this code which I coded earlier and have problem testing it or it's incomplete, I want to add the month February specific days which has 28 days or 29 days (leap year or not)
public void addDays(int x) {
day = day + x;
if (day > 31) {
day = day - 31;
addMonths(1);
} else if (month == 2 && ((year % 400 == 0) || year % 4 == 0)) {
day = day - 29;
addMonths(1);
}
add days wraps months,
add days wraps february non leap year,
add days wraps february leap year,
noting that I have already defined the variables day,month and year in another method as well as the other methods in the code and they work perfectly. Just having problem adding wraps for february leap year or not
Thanks.
P.S: I'm using this specific methods and classes because of the assignment requirements otherwise i would use the Calender class.
Check this:
import java.util.Arrays;
import java.util.List;
public class DaysAdder {
int day=1;
int month=1;
int year=2014;
public DaysAdder(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public void addDays(int x) {
day = day + x;
if (day > 29 && month == 2 && isLeapYear()) {
day = day - 29;
addMonths(1);
} else if (day > 28 && month == 2 && !isLeapYear()) {
day = day - 28;
addMonths(1);
} else if (day > 30 && !is31dayMonth()) {
day = day - 30;
addMonths(1);
} else if (day > 31 && is31dayMonth()) {
day = day - 31;
addMonths(1);
}
}
private boolean is31dayMonth() {
List<Integer> month31 = Arrays.asList(1,3,5,7,8,10,12);
return month31.contains(month);
}
private boolean isLeapYear() {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
private void addMonths(int i) {
month = month + i;
if (month >= 13) {
addYear(1);
month = month - 12;
}
}
private void addYear(int i) {
year = year + i;
}
#Override
public String toString() {
return "DaysAdder [day=" + day + ", month=" + month + ", year=" + year
+ "]";
}
public static void main(String[] args) {
DaysAdder daysAdder = new DaysAdder(23, 2, 2016);
daysAdder.addDays(7);
System.out.println(daysAdder); //DaysAdder [day=1, month=3, year=2016]
daysAdder = new DaysAdder(23, 2, 2014);
daysAdder.addDays(7);
System.out.println(daysAdder); //DaysAdder [day=2, month=3, year=2014]
}
}
Noob here so bear with me..I was working on a Java program that takes the users input from the JoptionPane, and uses a set of if-else statements to verify the date while considering the leap years. The problem I seem to be having when it runs is that it validates dates that aren't possible. For example, 2/29/2014, it says it's a valid date, but in reality it isn't.
Insight on this issue would be highly appreciated.
import java.util.Arrays;
import javax.swing.JOptionPane;
public class vaildDate {
public static void main(String[] args) {
// TODO Auto-generated method stub
int month, day, year;
String userInput = JOptionPane.showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
String [] date = userInput.split("/");
String mm = date[0];
String dd = date [1];
String yyyy = date [2];
month = Integer.parseInt(mm);
day = Integer.parseInt(dd);
year = Integer.parseInt(yyyy);
System.out.println(mm+dd+yyyy);
boolean validLeapYear;
boolean validDate;
if(month>=1 && month <=12&& day>=1 && day<=31)
{
if(month == 4 || month == 9 || month == 6 || month == 11 && day <= 30)
{
validDate = true;
}
if(( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 11 || month == 12) && (day <= 31))
{
validDate = true;
}
else{
validDate =false;
}
if((year % 400 == 0 ) || (year % 4 == 0) && (year % 100 != 0))
{
validLeapYear = true;
}
if ((month == 2 && day == 29) && (validLeapYear = false ))
{
validDate = false;
}
if((month == 2 && day <= 29) && (validLeapYear = true ))
{
validDate = true;
}
if(validDate = true)
{
JOptionPane.showMessageDialog(null, month +"/"+day+"/"+year+" is a valid date");
}
else if(validDate = false) {
JOptionPane.showMessageDialog(null,"Your date is invalid");
}
}
}
}
Instead of equating booleans your are assigning the values which return the results of your assignment.
For example:
if (validDate = true)
The above statement would return true. Instead, in all your boolean checks just validate the boolean value as is.
if (validDate)
To remove the compiler errors, you can initialize your 2 boolean vars to false.
You changed code would look something like:
public static void main(String[] args) {
// TODO Auto-generated method stub
int month, day, year;
String userInput = JOptionPane
.showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
String[] date = userInput.split("/");
String mm = date[0];
String dd = date[1];
String yyyy = date[2];
month = Integer.parseInt(mm);
day = Integer.parseInt(dd);
year = Integer.parseInt(yyyy);
System.out.println(mm + dd + yyyy);
boolean validLeapYear = false;
boolean validDate = false;
if (month >= 1 && month <= 12 && day >= 1 && day <= 31) {
if (month == 4 || month == 9 || month == 6 || month == 11
&& day <= 30) {
validDate = true;
}
if ((month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 11 || month == 12)
&& (day <= 31)) {
validDate = true;
} else {
validDate = false;
}
if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)) {
validLeapYear = true;
}
if ((month == 2 && day == 29) && (!validLeapYear)) {
validDate = false;
}
if ((month == 2 && day <= 29) && (validLeapYear)) {
validDate = true;
}
if (validDate) {
JOptionPane.showMessageDialog(null, month + "/" + day + "/"
+ year + " is a valid date");
}
else if (!validDate) {
JOptionPane.showMessageDialog(null, "Your date is invalid");
}
}
}
A quick search on Google shows there are easier ways to validate a date. Please see this one:
http://www.mkyong.com/java/how-to-check-if-date-is-valid-in-java/
It uses the date parse and catches the exception if the date is not valid (cannot be parsed).
If you want to stick with your algorithm, I suggest to use an IDE like Eclipse and debug set by step while keeping a look on the value of your variables. You clearly have an algorithm problem, which will become obvious while debugging.
Hope that helps.