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
Related
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;
}
}
I am trying to make a program that gets the day from the user input and then tells them the day before and the day after.The user should also be able to input how many days to add and the program should output that day.
example user enters 1 = Monday, tomorrow is = 2 Tuesday yesterday was = 3 Sunday
if user says its Monday(1) and adds 12 days the output should be Saturday(6)
The problem is whenever "theWeekDay" is greater than 7 it outputs nothing because TheDay(); doesn't have a condition for something greater than 7. Please help me!
Thanks you so much!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
If you want to assume value greater that 7 as valid, you should definitely use modulo operation. Something like this:
if(theWeekDay > 7) {
theWeekDay = theWeekDay % 7;
}
Otherwise you should throw and exception.
your if else must cover all cases ....
add an else after
else if(theWeekDay == 7){
System.out.println("Sunday");
}
something like:
if(theWeekDay == 1){
System.out.println("Monday");
}else if(theWeekDay == 2){
System.out.println("Tuesday");
}else if(theWeekDay == 3){
System.out.println("Wednsday");
}else if(theWeekDay == 4){
System.out.println("Thursday");
}else if(theWeekDay == 5){
System.out.println("Friday");
}else if(theWeekDay == 6){
System.out.println("Saturday");
}else if(theWeekDay == 7){
System.out.println("Sunday");
}else{
System.out.println("Invalid input");
}
return theWeekDay;
As user629735 said, use modulo in your formula.
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = (theWeekDay + AddedDays) % 7;
return TheDay(theWeekDay);
}
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
}
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
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.