Reputation: 1
im using jdk se and i keep getting a parsing issue
"error: reached end of file while parsing"
i dont really understand why i keep getting this issue i have my class closed and brackets in places i think i would need brackets.
import java.util.Scanner;
public class days_in_a_month {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a month:");
String month = input.nextLine();
input.nextLine();
System.out.print("Please enter a year:");
String year = input.nextLine();
input.nextLine();
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);
switch (month){
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
System.out.println(month + " " + year + " has 31 days"); break;
case "4":
case "6":
case "9":
case "11":
System.out.println(month + " " + year + " has 30 days"); break;
case "2":
if(isLeapYear)
{
System.out.println(month + " " + year + " has 29 days"); break;
}
else
{
System.out.println(month + " " + year + " has 28 days");
}
}
}
Upvotes: 0
Views: 962
Reputation: 360
The Best of The Best solve is:
package com.dayinmonth
import java.time.LocalDate;
import java.util.Scanner;
public class days_in_a_month {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a month:");
int month = input.nextInt();
input.nextLine();
System.out.print("Please enter a year:");
int year = input.nextInt();
input.nextLine();
LocalDate date = LocalDate.of(year, month, 1);
System.out.println(String.format("%d %d has %d days", date.getMonthValue(), date.getYear(), date.lengthOfMonth()));
}
}
Upvotes: 0
Reputation: 316
the best way to solve is using some like this:
int year = input.nextInt();
Upvotes: 2
Reputation: 360
just define year variable as int:
int year = Integer.valueOf(input.nextLine());
The problem is that you cannot use % operator for Strings
Here is the full example:
package com.yourpackage;
import java.util.Scanner;
public class days_in_a_month {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a month:");
String month = input.nextLine();
input.nextLine();
System.out.print("Please enter a year:");
int year = Integer.valueOf(input.nextLine());
input.nextLine();
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
switch (month) {
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
System.out.println(month + " " + year + " has 31 days");
break;
case "4":
case "6":
case "9":
case "11":
System.out.println(month + " " + year + " has 30 days");
break;
case "2":
if (isLeapYear) {
System.out.println(month + " " + year + " has 29 days");
break;
} else {
System.out.println(month + " " + year + " has 28 days");
}
}
}
}
Don't forget to name your file for as "days_in_a_month" and to define the appopriate package in the top of the file!
Upvotes: 0
Reputation: 5712
In your code you are trying to use %
operator with String
boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Operator %
can't be used with String
Upvotes: 0