Unable to print correct Calendar on Java - java

Can anyone help me find what is wrong with my code? I cannot figure out how to display the correct start day of the month (Note that the January 1, 1900 was a Monday) or to display the correct number of days in the month of February if the year is a leap year.
public class Calendar {
/**
*
*
* #param s
* #return
*/
public static int getMonthNumber(String s){
if (s.equalsIgnoreCase("jan"))
return 1;
if (s.equalsIgnoreCase("feb"))
return 2;
if (s.equalsIgnoreCase("mar"))
return 3;
if (s.equalsIgnoreCase("apr"))
return 4;
if (s.equalsIgnoreCase("may"))
return 5;
if (s.equalsIgnoreCase("jun"))
return 6;
if (s.equalsIgnoreCase("jul"))
return 7;
if (s.equalsIgnoreCase("aug"))
return 8;
if (s.equalsIgnoreCase("sep"))
return 9;
if (s.equalsIgnoreCase("oct"))
return 10;
if (s.equalsIgnoreCase("nov"))
return 11;
if (s.equalsIgnoreCase("dec"))
return 12;
else
System.out.println("Not valid month!");
return 0;
}
public static boolean isLeapYear(int year){
int month = 0;
int s = getDaysIn(month, year);
return year%4==0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn (int month, int year){
switch (month) {
case 1: return 31;
case 2: if(isLeapYear(month)) return 29;
else return 28;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
default: return -1;
}
}
public static String getMonthName (int month){
switch (month) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid month.";
}
}
public static int getStartDay (int month, int year){
int days = 0;
for (int i = 1900; i<year; i++){
days = days + 365;
if (isLeapYear(i))
days = days + 1;}
for (int i=1; i<month; i++)
days = days + getDaysIn(month, year);
int startday = (days + 1)%7+1;
return startday;
}
public static void displayCalendar(int month, int year){
String monthname=getMonthName(month);
int startDay= getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay-1;
for (int i=1; i<=startDay; i=i+1)
System.out.print(" ");
for ( int x=1; x<=monthDays; x++){
weekDay = weekDay +1;
if (weekDay>7){
System.out.println();
weekDay = 1;}
System.out.format(" %3d",x);
}
if (weekDay > 7){
System.out.println();
}
}
public static void main(String[] args){
Scanner c = new Scanner (System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
String month,year;
month=c.next();
year=c.next();
int yearno =Integer.parseInt(year);
int monthno = getMonthNumber(month);
displayCalendar(monthno, yearno);
}
}

Suppose that you can use the java.util.Calendar, I would code something like this:
import java.util.Calendar;
import java.util.Scanner;
public class CalendarPrinter {
private Calendar c = null;
CalendarPrinter(int month, int year ){
c = Calendar.getInstance();
c.clear();
c.set(Calendar.MONTH, month);
c.set(Calendar.YEAR, year);
}
public String showCalendar(){
StringBuffer calendarTxt = new StringBuffer("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
int daysInAMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = c.getFirstDayOfWeek();
int i = 1;
do {
if (i < c.get(Calendar.DAY_OF_WEEK)){
calendarTxt.append("\t");
daysInAMonth++;
} else {
calendarTxt.append(firstDayOfWeek+"\t");
firstDayOfWeek++;
}
if (i%7 == 0 && i != 1){
calendarTxt.append("\n");
}
i++;
} while (i <= daysInAMonth);
return calendarTxt.toString();
}
public static int getMonthNumber(String s){
if (s.equalsIgnoreCase("jan"))
return 0;
if (s.equalsIgnoreCase("feb"))
return 1;
if (s.equalsIgnoreCase("mar"))
return 2;
if (s.equalsIgnoreCase("apr"))
return 3;
if (s.equalsIgnoreCase("may"))
return 4;
if (s.equalsIgnoreCase("jun"))
return 5;
if (s.equalsIgnoreCase("jul"))
return 6;
if (s.equalsIgnoreCase("aug"))
return 7;
if (s.equalsIgnoreCase("sep"))
return 8;
if (s.equalsIgnoreCase("oct"))
return 9;
if (s.equalsIgnoreCase("nov"))
return 10;
if (s.equalsIgnoreCase("dec"))
return 11;
else
System.out.println("Not valid month!");
return 0;
}
public static void main(String[] args){
Scanner input = new Scanner (System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
String month;
int year;
month=input.next();
year=input.nextInt();
CalendarPrinter c = new CalendarPrinter(getMonthNumber(month), year);
System.out.println(c.showCalendar());
}
}

Here is an example just for days of week based on Zeller's congruence.
Sample code:
class Ideone
{
public static void main (String[] args) {
System.out.println(days()[dow(8,10,2015)]);
}
public static String[] days() {
return new String[] {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
}
public static int dow(int d, int m, int y) {
if (m < 3) {
m += 12;
y--;
}
return (d + (int)((m+1)*2.6) + y + (int)(y/4) + 6*(int)(y/100) + (int)(y/400) + 6) % 7;
}
}
Running code here
Code reference here

Related

Variable Mismatch with case

I am having difficulty with a java problem I am attempting to make,
I am very new and still learning if anyone can spot what I am doing wrong I would appreciate any help.
I am attempting to have the user type in a year from 2000 to 2016 and the output would be the players statistics.
Thank You
import java.util.Scanner;
public class NflPlayers{
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a Year (2000-2016): " );
String year = input.next();
input.close();
if (yearNumber(year) == 0) {
System.out.println("Year 2000-2016");
System.out.println("Invalid year input.");
System.exit(0);
}
System.out.println(year + " Passing touchdowns");
}
int getNumberOfPasses(int year) {
switch (yearNumber(null)) {
case 1: return 28;
case 2: return 36;
case 3: return 33;
case 4: return 25;
case 5: return 34;
case 6: return 39;
case 7: return 36;
case 8: return 28;
case 9: return 0;
case 10: return 50;
case 11: return 24;
case 12: return 26;
case 13: return 28;
case 14: return 23;
case 15: return 28;
case 16: return 18;
case 17: return 0;
default: return 0;
}
}
public static int yearNumber(String year) {
int yearNumber;
switch (year){
case"2016":
yearNumber = 1;
break;
case"2015":
yearNumber = 2;
break;
case"2014":
yearNumber = 3;
break;
case"2013":
yearNumber = 4;
break;
case"2012":
yearNumber = 5;
break;
case"2011":
yearNumber = 6;
break;
case"2010":
yearNumber = 7;
break;
case"2009":
yearNumber = 8;
break;
case"2008":
yearNumber = 9;
break;
case"2007":
yearNumber = 10;
break;
case"2006":
yearNumber = 11;
break;
case"2005":
yearNumber = 12;
break;
case"2004":
yearNumber = 13;
break;
case"2003":
yearNumber = 14;
break;
case"2002":
yearNumber = 15;
break;
case"2001":
yearNumber = 16;
break;
case"2000":
yearNumber = 17;
break;
default:
yearNumber = 0;
break;
}
return yearNumber;
}
}
You ca use do{}while() loop instead for example :
Scanner input = new Scanner(System.in);
String year;
boolean pass = false;
do {
System.out.print("Enter a Year (2000-2016): ");
year = input.next();
try {
if (Integer.parseInt(year) >= 2000 && Integer.parseInt(year) <= 2006) {
System.out.println("true");
pass = true;
}
} catch (NumberFormatException e) {
System.out.println("Not correct try again");
}
} while (!pass);
So if the user enter a year between 2000 and 2006 you can pass, else repeat again until the user enter the correct year.
I've run your code and examined it.
It appears you've left out a method call for getNumberOfPasses(), which is the main problem I can tell.
with System.out.println(year + " Passing touchdowns"); you don't actually want the year displayed, i believe the correct code you're looking for is:
System.out.println(getNumberOfPasses(yearNumber(year)) + " Passing touchdowns");
This will run through both methods to display the info you need. However, you will need to fix the method header of int getNumberOfPasses(int year) to static int getNumberOfPasses(int year)
Furthermore, you could eliminate one of the methods entirely and just have the return values for passes tied to the year you input. Rather than passing digits from 1-17 to another method.
Hope this helps and good luck with your projects!

How to use switch-statement correctly?

I need to write a Java program (class NextDay) that calculates and prints the date of the next day by entering the day, month, and year.
Sample call and output:
Actual Date 12.12.2004
The next day is the 13.12.2004.
The input should also be checked for correctness! If an erroneous date constellation (e.g., 32 12 2007) has been entered, the exception InvalidDateArgumentsException is to be thrown. Define this class in a suitable form as a subclass of the class java.lang.Exception.
I started to creating it but the problem is; i cant tell that < or > in switch statements.What should i do? Here are my classes:
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public Date getNextDay() throws Exception {
if (isLeapYear() == true) {
switch (month) {
case 1:
day = 31;
break;
case 2:
day = 29;
break;
case 3:
day = 31;
break;
case 4:
day = 30;
break;
case 5:
day = 31;
break;
case 6:
day = 30;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 9:
day = 30;
break;
case 10:
day = 31;
break;
case 11:
day = 30;
break;
case 12:
day = 31;
break;
}
}
return new Date(day + 1, month, year);
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public boolean isLeapYear() {
if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0) {
return true;
}
return false;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
}
...
public class NextDay {
public static void main(String args[]) throws Exception {
Date dateObj = new Date(20, 5, 2016);
System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + ".");
System.out.println("The next day is " + dateObj.getNextDay().toString() + ".");
}
}
You are saying that you want an "or" statement inside switch, right? If you write case labels line by line you get "or statement", like this:
switch(variable){
case 1:
case 2:
case 3: {
//.. when variable equals to 1 or 2 or 3
}
}
So that, you can write your getMaxDaysInMonth method like this:
int getMaxDaysInMonth()
{
int daysInMonth = 0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 2:
if(isLeapYear())
{
daysInMonth = 29;
}
else
{
daysInMonth = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
}
return daysInMonth;
}
Also, you are checking for the leap year incorrectly. Here's the correct way:
boolean isLeapYear(){
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
And here's how you increment a day:
void incrementDate(){
if((day + 1) > getMaxDaysInMonth())
{
day = 1;
if (month == 12)
{
month = 1;
year++;
}else{
month++;
}
} else {
day++;
}
}
EDIT since we can't just use standard classes
If you have to use switch case, you should use it to set max day in current month and then check, if your current day more than this max day:
int maxDay;
switch (month) {
case 1: maxDay = 31; break;
case 2: maxDay = isLeapYear() ? 29 : 28; break;
case 3: maxDay = 31; break;
// ... other months ...
case 12: maxDay = 31; break;
default: throw new InvalidDateArgumentsException();
}
if (isLeapYear()) {
maxDay = 29;
}
if (day > maxDay) {
throw new InvalidDateArgumentsException();
}

illegal start of expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have been searching all around the web to find a solution but no
luck was found. So i went to stack overflow to see some solutions but still no luck so i am posting my code to share and help me fix this. Thank you Guys!
public class MyDate {
private int month;
private int day;
private int year;
private String MoInTxt;
private int dayz;
public MyDate(){
this(1, 1, 1990);
}
public MyDate(int d, int m, int y){
setDay(d);
setMonth(m);
setYear(y);
}
// Setter
public String MonthToTxt() {
int mo = month;
switch (mo) {
case 1: MoInTxt = "January";
break;
case 2: MoInTxt = "February";
break;
case 3: MoInTxt = "March";
break;
case 4: MoInTxt = "April";
break;
case 5: MoInTxt = "May";
break;
case 6: MoInTxt = "June";
break;
case 7: MoInTxt = "July";
break;
case 8: MoInTxt = "August";
break;
case 9: MoInTxt = "September";
break;
case 10: MoInTxt = "October";
break;
case 11: MoInTxt = "November";
break;
case 12: MoInTxt = "December";
break;
default: MoInTxt = "Invalid month";
break;
}
}
public void setMonth(int m) {
if(m>=1 && m <=12) {
month = m;
if(m ==2 && LeapChecker(year) == true) {
month = 29;
}else{
month = 28;
}
}else {
month = 1;
}
}
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
}
public void setYear(int yr) {
if (yr >=1900){
year = yr;
} else {
year = 1900;
}
}
// Getter
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() {
return year ;
}
//==============================//
public String getCompleteDate(){
return String.format ("%s %02d, %d", MoInTxt, day, year);
}
//==============================//
public void addDays(int dey) {
int addedDey = dey + day;
int ToAddMos=0;
if(addedDey <=28) {
setDay(addedDey);
} else {
while(addedDey>28){
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month == 12){
if(addedDey>31){
addedDey=addedDey-31;
setDay(addedDey);
ToAddMos++;
}else{
setDay(addedDey);
break;
}
}else if(month ==2) {
if(LeapChecker(year) == true){
if(addedDey >29) {
addedDey = addedDey - 29;
ToAddMos++;
}else {
setDay(addedDey);
break;
}
}else if(LeapChecker(year) == false) {
if(a>28){
addedDey = addedDey - 28;
ToAddMos++;
}else{
setDay(AddedDey);
break;
}
}
}else if(month == 4 || month == 6 || month == 9 || month == 11){
if(addedDey>30){
addedDey-=30;
ToAddMos++;
} else {
setDay(addedDey);
break;
}
}
}
}
}
public void addMonths(int mon) {
int addedMon = mon + month;
setMonth(addedMon);
}
public void addYears(int yir) {
int addedYir = year + yir;
setYear(addedYir);
}
/* Method Name: Leap Checker
* This Method checks if that year is a leap year or not
* 1. It checks if the year is divisible by 400
* 2. It checks if the year is divisible by 4
* 3. It checks if the year is not divisible by 100
*/
public boolean LeapChecker(int year){
year = year;
boolean isLeap =true;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
isLeap = true;
} else {
isLeap = false;
}
return isLeap;
}
}
}
The problem is a missing closing curly brace at the end of the setDay() method:
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
} // <------------------ this one
}
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
}//Missing closing the else block
}

printing out the day of the week in Java

Im trying to get my program to print out the day of any given date using functions that I have to write and declare, although for the early dates of the month the program doesnt seem to print the write day. The equation in the dayOfTheWeek function, w, was given to us to calculate for the day, although for the 'floor' code to be used i had to create another 'private static' function for reasons im not quite sure of, any reason as to why would be great as well as any reason for why my program isnt returning the right day for certain dates.
here's my code, any help would be greatly appreciated :)
import java.util.Scanner;
import javax.swing.JOptionPane;
public class DayOfTheWeek {
public static final int SEPTEMBER_APRIL_JUNE_NOVEMBER_DAYS = 30;
public static final int REST_OF_YEAR_DAYS = 31;
public static final int LEAP_YEAR_FEB = 29;
public static final int NORMAL_FEB = 28;
public static final int MONTHS = 12;
public static void main(String[] args) {
try
{
String input = JOptionPane.showInputDialog("Enter date (day/month/year):");
Scanner scanner = new Scanner( input );
scanner.useDelimiter("/");
int day = scanner.nextInt();
int month = scanner.nextInt();
int year = scanner.nextInt();
scanner.close();
String numberEnding = numberEnding (day);
String dayEnding = day + numberEnding;
String monthName = monthName (month);
String dayName = dayOfTheWeek (day, month, year);
if (validDate(day, month, year))
{
JOptionPane.showMessageDialog(null, dayName + " " + dayEnding + " " + monthName
+ " " + year + " is a valid date.");
}
else
{
JOptionPane.showMessageDialog(null, "" + dayEnding + " " + monthName
+ " " + year + " is not a valid date.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
catch (NullPointerException exception)
{
}
catch (java.util.NoSuchElementException exception)
{
JOptionPane.showMessageDialog(null, "No number entered. \nPlease restart and try again.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public static boolean validDate( int day, int month, int year ) {
return ((year >= 0) && (month >= 1) && (month <= MONTHS) &&
(day >= 1) && (day <= daysInMonth( month, year )));
}
public static int daysInMonth( int month, int year ) {
int monthDays;
switch (month)
{
case 2:
monthDays = isLeapYear(year) ? LEAP_YEAR_FEB : NORMAL_FEB;
break;
case 4:
case 6:
case 9:
case 11:
monthDays = SEPTEMBER_APRIL_JUNE_NOVEMBER_DAYS;
break;
default:
monthDays = REST_OF_YEAR_DAYS;
}
return monthDays;
}
public static boolean isLeapYear( int year ) {
return (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
}
public static String numberEnding( int day ) {
String dayEnding = "";
int remainder = day%10;
if (day >= 10 && day <= 20)
{
dayEnding = "th";
}
else
{
switch (remainder)
{
case 1:
dayEnding = "st";
break;
case 2:
dayEnding = "nd";
break;
case 3:
dayEnding = "rd";
break;
default:
dayEnding = "th";
break;
}
}
return dayEnding;
}
public static String monthName( int month ) {
String monthName = "";
switch (month)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
}
return monthName;
}
public static String dayOfTheWeek (int day, int month, int year){
String dayName = "";
int Y;
if (month == 1 || month == 2)
{
Y = (year-1);
}
else
{
Y = (year);
}
int y = Y%100;
int c = Y/100;
int w = (day + floor(2.6 * (((month+9) % 12)+ 1) -0.2)
+ y + floor(y/4) + floor(c/4) - (2*c));
w = (w%7);
if (w < 0)
{
w += 7;
}
switch (w)
{
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
}
return dayName;
}
private static int floor(double d) {
return 0;
}
}
I believe you need to use the Math.floor() method. Simply call this in place of your floor method:
(day + Math.floor(2.6 * (((month+9) % 12)+ 1) -0.2)
+ y + Math.floor(y/4) + Math.floor(c/4) - (2*c));
You can also cast the equation directly using (int):
int w = (int) (day + 2.6 * ((month+9) % 12 + 1) - 0.2 + y + (y/4) + (c/4) - (2*c));
However, in your case I think that the values will be rounded improperly using casting, so you should probably use the floor method.
If you'd like some additional information on the differences between floor and casting here's a stackoverflow question that addresses it: Cast to int vs floor
I would use the Joda-Time library.
import org.joda.time.DateTime
final DateTime date = new DateTime();
final int dayOfWeek = date.getDayOfWeek();
See the Joda-Time User Guide for more info and examples..

unable to use string.nextInt in string.hasNextLine loop

I need to create a program that receives the dates from file dates.txt and outputs them to dates.out, if the date is valid, then it will provide a new format and the day that the date falls in the year, if not it will return "Invalid date: (original date and format)" it seems that I have it all completed below, but I keep getting the error below when I type in the correct file name, im not sure what the problem is since I'm calling for a valid int.
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
Input file name: dates.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Lab12.main(Lab12.java:43)
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
import java.io.*;
import java.util.*;
public class Lab12{
public static final int JAN = 1;
public static final int FEB = 2;
public static final int MAR = 3;
public static final int APR = 4;
public static final int MAY = 5;
public static final int JUN = 6;
public static final int JUL = 7;
public static final int AUG = 8;
public static final int SEP = 9;
public static final int OCT = 10;
public static final int NOV = 11;
public static final int DEC = 12;
public static void main(String[] arg)
throws FileNotFoundException{
Scanner console = new Scanner(System.in);
Scanner input = getInput(console);
while(input.hasNextLine()){
String text = input.nextLine();
Scanner time = new Scanner(text);
int day = time.nextInt(); // <<<< line 43
int month = time.nextInt();
int year = time.nextInt();
if(validDate(day, month, year)){
output.print (formatDate(day, month, year));
}
else
output.print ("Invalid date: "+day+"/"+month+"/"+year);
}
}
public static Scanner getInput(Scanner console)
throws FileNotFoundException{
System.out.print ("Input file name: ");
File f = new File(console.nextLine());
while(!f.canRead()){
System.out.println ("File not found. Try again.");
System.out.print ("Input file name: ");
f = new File(console.nextLine());
}
return new Scanner(f);
}
public static boolean isLeapYear(int year){
boolean isLeapYear = false;
if(year % 400 == 0)
isLeapYear = true;
else if(year % 4 == 0 && year % 100 == 0)
isLeapYear = false;
else if(year % 4 == 0)
isLeapYear = true;
else
isLeapYear = false;
return isLeapYear;
}
//http://www.java-forums.org/new-java/41020-day-number-count.html
public static int dayNumber(int day, int month, int year){
int daysInMonth = 0;
int days = 0;
boolean leapYear = isLeapYear(year);
for(int i = 1; i < month; i++){
switch(month){
case 1: daysInMonth += 31;
break;
case 2: if(leapYear)
daysInMonth = 29;
else
daysInMonth = 28;
break;
case 3: daysInMonth += 31;
break;
case 4: daysInMonth += 30;
break;
case 5: daysInMonth += 31;
break;
case 6: daysInMonth += 30;
break;
case 7: daysInMonth += 31;
break;
case 8: daysInMonth += 31;
break;
case 9: daysInMonth += 30;
break;
case 10: daysInMonth += 31;
break;
case 11: daysInMonth += 30;
break;
case 12: daysInMonth += 31;
break;
default:
break;
}
while(month <= 12){
days += days + daysInMonth + day;
month++;
}
}
return days;
}
public static boolean validDate(int day, int month, int year){
boolean validDate = false;
if(year >= 1 && year <= 3000){
if(month >= 1 && month <= 12){
if(month == 4 || month == 6 || month == 9 || month == 11
&& day >= 1 && day <= 30){
validDate = true;
}else if(month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12 &&
day <=31){
validDate = true;
}else if(month == 2){
boolean leapYear = isLeapYear(year);
if(leapYear = true && day >= 28){
validDate = false;
}
else if(leapYear = false && day <= 29){
validDate = true;
}
else{
validDate = true;
}
}
}
else
validDate = false;
}
else
validDate = false;
return validDate;
}
public static String formatDate(int day, int month, int year){
String formatDate = String.format ("%d-Jan-%d", day, year);
return formatDate;
}
}
content of dates.txt file
10/1/1999
12/31/2000
2/29/1900
2/1/1996
1/1/2097
2/29/2000
7/4/1776
5/32/3001
0/2/1234
8/0/2345
9/30/3001
2/29/2010
3/31/2001
13/3/1867
12/31/3000
You need to get the input as a String
"2/4/05" Them parse it
String dateString = input.nextLine();
String[] tokens = dateString.split("/");
int month = Integer.parseInt(tokens[0]);
int day = Integer.parseInt(tokens[1]);
int year = Integer.parseInt(tokens[2])
You're getting the error because of the /'s in the date.

Categories