illegal start of expression [closed] - java

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
}

Related

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

Syntax error on token “Invalid Character”, delete this token

I had a problem with my Eclipse, I am using MAC OS.
I don't know why it says error: "Syntax error on token “Invalid Character”, delete this token". May anyone tell me why? I've googling somewhere but can't found how to solve this issue.
Here is my code:
package _Java_tuan1;
import java.util.Scanner;
public class Date {
private String month;
private int day, year;
public Date(){
month = "January";
day = 1;
year = 1000;
}
public Date(int monthInt, int day, int year){
setDate(monthInt, day, year);
}
public Date(String monthString, int day, int year){
setDate(monthString, day, year);
}
public Date(int year){
setDate(1, 1, year);
}
public Date(Date aDate){
if (aDate == null){
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}
public void setDate(int monthInt, int day, int year){
if(dateOK(monthInt, day, year)){
this.month = monthString(monthInt);
this.day = day;
this.year = year;
}else{
System.out.println("Fatal Error");
System.exit(0);
}
}
public void setDate(String monthString, int day, int year){
if(dateOK(monthString, day, year)){
this.month = monthString;
this.day = day;
this.year = year;
}
else{
System.out.println("Fatal Error");
System.exit(0);
}
}
public void setDate(int year){
setDate(1, 1, year);
}
public void setYear(int year){
if( (year < 1000) || (year > 9999)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
this.year = year;
}
}
public void setMonth(int monthNumber){
if((monthNumber <= 0) || (monthNumber > 12)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
month = monthString(monthNumber);
}
}
public void setDay(int day){
if((day <= 0) || (day > 31)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
this.day = day;
}
}
public int getMonth(){
if(month.equals("January")){
return 1;
}
else if(month.equals("February")){
return 2;
}
else if(month.equals("March")){
return 3;
}
else if(month.equals("April")){
return 4;
}
else if(month.equals("May")){
return 5;
}
else if(month.equals("June")){
return 6;
}
else if(month.equals("July")){
return 7;
}
else if(month.equals("August")){
return 8;
}
else if(month.equals("Septemper")){
return 9;
}
else if(month.equals("October")){
return 10;
}
else if(month.equals("November")){
return 11;
}
else if(month.equals("December")){
return 12;
}
else{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
public String toString(){
return (month +" "+day+", "+year);
}
public boolean equals(Date otherDate){
 return ((month.equals(otherDate.month)) && (day == otherDate.day) && (year == otherDate.year));
}
public boolean precedes(Date otherDay){
return ( (year < otherDay.year) || (year == otherDay.year) && getMonth() < otherDay.getMonth() ||
(year == otherDay.year && month.equals(otherDay.month) && day < otherDay.day));
}
public void readInput(){
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
while(tryAgain){
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
String monthInput = keyboard.next();
int dayInput = keyboard.nextInt();
int yearInput = keyboard.nextInt();
if (dateOK(monthInput, dayInput, yearInput) ){
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}
private boolean dateOK(int monthInt, int dayInt, int yearInt){
return ( (monthInt >= 1) && (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (yearInt <= 9999) );
}
private boolean dateOK(String monthString, int dayInt, int yearInt){
return (monthOK(monthString) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (yearInt <= 9999));
}
private boolean monthOK(String month){
return (month.equals("January")
|| month.equals("February")
|| month.equals("March")
|| month.equals("April")
|| month.equals("May")
|| month.equals("May")
|| month.equals("May")
|| month.equals("June")
|| month.equals("July")
|| month.equals("August")
|| month.equals("Septemper")
|| month.equals("Octobor")
|| month.equals("November")
|| month.equals("December"));
}
private String monthString(int monthNumber){
switch(monthNumber){
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 "Septemper";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}
}
Have you pasted the code from Windows?
Delete the last few characters and re-enter them - I think it will go away

Unable to print correct Calendar on 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

Having problems with consistency [closed]

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 7 years ago.
Improve this question
I'm having issues with my program. Namely in that when I enter a value to say what day it will be a few days from now, it sometimes work, and other times it doesn't. I'm not sure what the problem is... Bear with me I'm still learning.
Thanks in advance
import java.util.*;
public class pooped {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int day;
int june;
int dates;
System.out.println(" Days of the week are numbered 0 - 6 " +
"From Sunday to Saturday, enter a number now");
day = console.nextInt();
int kill = day;
System.out.println("Enter the number of days forward: ");
dates = console.nextInt();
printday(day);
day = addDay(day);
printday(day);
day = removeDay(day);
printday(day);
dates = count(dates);
printday(kill + dates);
}
public static int count(int dates) {
if (dates > 6){
dates = (dates % 6);
}
System.out.println("That many days out is: ");
return dates;
}
private static int addDay(int day) {
day++;
System.out.println("The next day is: ");
if (day > 6) {
day = 0;
}
return day;
}
private static int removeDay(int day) {
day = day - 2;
System.out.println("The previous day is: ");
if (day == 0) {
day = 6;
}
return day;
}
public static boolean isWeek(int day) {
return day >= 0 && day <= 6;
}
public static void printday(int day) {
switch (day)
{
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
default:
break;
}
}
}
You should mod by 7, not 6. Modding by 6 will only yield values 0-5.
dates %= 7;
So, whenever you add or subtract from a variable that should remain between 0-6, perform the above mod, by 7.
Full source
public class Main {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int day;
int june;
int dates;
System.out.println(" Days of the week are numbered 0 - 6 "
+ "From Sunday to Saturday, enter a number now");
day = console.nextInt();
System.out.println("Enter the number of days forward: ");
dates = console.nextInt();
printday(day);
printday(addDay(day));
printday(removeDay(day));
printday(day + count(dates));
}
public static int count(int dates) {
dates %= 7;
System.out.println("That many days out is: ");
return dates;
}
private static int addDay(int day) {
day++;
day %= 7;
System.out.println("The next day is: ");
return day;
}
private static int removeDay(int day) {
day--;
day += 7;
day %= 7;
System.out.println("The next day is: ");
return day;
}
public static boolean isWeek(int day) {
return day >= 0 && day <= 6;
}
public static void printday(int day) {
switch (day) {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
default:
break;
}
}
}

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