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");
}
}
}
the best way to solve is using some like this:
int year = input.nextInt();
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
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!
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()));
}
}
Related
How do i take the user inputted day, month and year then store in a dd-mm-yyyy format? I can get everything to work except combining them into a date and storing them in the startDate variable. I also don't think startMonth will be accessible as it's in a switch case but I'm very new to java and unsure.
public static void carBookingDates() {
int carNumber;
int startYear;
int startDay;
int startMonth;
int daysInMonth = 0;
int endYear;
int endMonth;
int endDay;
Scanner input = new Scanner(System.in);
System.out.println("To make a booking:");
System.out.printf(" Select a car number from the car list: ");
carNumber = input.nextInt();
System.out.println("");
System.out.println("Enter a booking start date.");
System.out.printf("Please enter the year - for example '2022': ");
startYear = input.nextInt();
while (startYear < 2022 || startYear > 2030) {
System.out.println("Invalid year, please try again: ");
System.out.printf("Please enter the year - for example '2022': ");
startYear = input.nextInt();
}
System.out.printf("Please enter the month - for example '6': ");
startMonth = input.nextInt();
while (startMonth < 1 || startMonth > 12) {
System.out.println("Invalid month, please try again: ");
System.out.printf("Please enter the month - for example '6': ");
startMonth = input.nextInt();
}
switch (startMonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
if (((startYear % 4 == 0)
&& !(startYear % 100 == 0))
|| (startYear % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.printf("Please enter the day number - for"
+ " example '18': ");
startDay = input.nextInt();
while (startDay > daysInMonth || startDay <= 0) {
System.out.println("Invalid day, plese try again");
System.out.printf("Please enter the day number - for"
+ " example '18': ");
startDay = input.nextInt();
LocalDate startDate() = LocalDate.parse(startDay + "-" + StartMonth "-" + startYear);
}
}
First of all, you can use the Java 8 Date Time API to get the number of days in a month instead of using a manual switch case for making the code more readable.
import java.time.YearMonth;
Then:
// 2021 is the year and 2 is the month
int daysInMonth = YearMonth.of(2021,2).lengthOfMonth());
Now to form the date in dd-mm-yyyy format :
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Then:
// pass your date value here under LocalDate.of method
LocalDate.of(2021,2,16).format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
This question already has answers here:
Convert string to day of week (not exact date)
(10 answers)
Closed 5 years ago.
If a user enters string, for example, "Sunday", how do I assign "Sunday" to an int value (such as zero)?
EDIT:
Here is the final code for calculating the day of the week after x number of days have elapsed. Not sure if this is the most efficient way.
Thank you for everyone's expertise!
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day: ");
String day = input.next();
int todayDay = changeToString(day);
System.out.println(todayDay);
}
public static int changeToString(String day) {
int dayName = 0;
switch (day) {
case "Monday":
dayName = 1;
break;
case "Tuesday":
dayName = 2;
break;
case "Wednesday":
dayName = 3;
case "Thursday":
dayName = 4;
case "Friday":
dayName = 5;
case "Saturday":
dayName = 6;
}
int dayselapsed = input.nextInt();
int futureday = (dayName + dayselapsed) % 7;
switch (futureday) {
case 0: System.out.println("In " + dayselapsed + " days, it will be Sunday");
break;
case 1: System.out.println("In " + dayselapsed + " days, it will be Monday");
break;
case 2: System.out.println("In " + dayselapsed + " days, it will be Tuesday");
break;
case 3: System.out.println("In " + dayselapsed + " days, it will be Wednesday");
break;
case 4: System.out.println("In " + dayselapsed + " days, it will be Thursday");
break;
case 5: System.out.println("In " + dayselapsed + " days, it will be Friday");
break;
case 6: System.out.println("In " + dayselapsed + " days, it will be Saturday");
break;
}
return 0;
}
}
You can use this method
public class Day {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day: ");
String day = input.next();
int todayDay = changeToString(day);
System.out.println(todayDay);
}
public static int changeToString(String day) {
int dayName = 0;
switch (day) {
case "Monday":
dayName = 1;
break;
case "Tuesday":
dayName = 2;
break;
......
}
return dayName;
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please select your operator:\n"
+ "1 for +\n" +
"2 for -\n" +
"3 for *\n" +
"4 for %\n" +
"");
operator = myScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" +
"y for yes\n" +
"n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput=='n') ask=false;
}
}
}
i got trouble in my switch case
if i press any number or letter somthing like 5 or 6 or... it should print you chose wrong operator.
i think problem is in my default but i don't know where is it?
Just reorder your code like this
`public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
System.out.println("please select your operator:\n"
+ "1 for +\n"
+ "2 for -\n"
+ "3 for *\n"
+ "4 for %\n"
+ "");
operator = myScanner.nextInt();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("you chose " + operator + " operator babe");
System.out.println("do yo want to continue?\n"
+ "y for yes\n"
+ "n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput == 'n') {
ask = false;
}
}
}`
and you'll be fine
as for my comment, if you want to validate the input the user does (for the option) before having the user input another 2 numbers, than, yeah you should actually programm it that way that the validation goes RIGHT AFTER the first userinput. HereĀ“s a slightly corrected version of your code.
public static void main(String[] args) {
int operator;
double result;
boolean ask = true;
Scanner numberScanner = new Scanner(System.in);
while (ask) {
System.out.println(
"please select your operator:\n" + "1 for +\n" + "2 for -\n" + "3 for *\n" + "4 for %\n" + "");
operator = numberScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
// Here was your "Mistake". You instantly started asking the user for another input,
// but actually wanted to ahve the switch statment here
switch (operator) {
case 1:
result = get_num1(numberScanner) + get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 2:
result = get_num1(numberScanner) - get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 3:
result = get_num1(numberScanner) * get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 4:
result = get_num1(numberScanner) % get_num2(numberScanner);
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" + "y for yes\n" + "n for no\n");
char askInput = numberScanner.next().charAt(0);
if (askInput == 'n')
ask = false;
}
}
public static double get_num1(Scanner scanner) {
System.out.println("please enter your first number");
return scanner.nextDouble();
}
public static double get_num2(Scanner scanner) {
System.out.println("please enter your second number");
return scanner.nextDouble();
}
simply you could validate the operator while you assign it with the input.
for example use if condition and check whether its between 1 and 5 and if not print whatever you want
2 things:
you dont need 2 scanners using only one will be enough
the code is behaving so because you go into the switch case AFTER asking the numbers you want to operate...
some condition like:
operator = myScanner.nextInt();
if (operator < 1 || operator > 4) {
}
may help....
I am currently trying to finish this program, however when I am testing my switch statement, it goes directly to my default case and says that I have entered invalid information.
My Task
I have to receive a month from the user and send it to my case statement in order to execute my code for the certain case. As you may notice, that each case has a key in it, this key is for personal purposes. Please disregard.
My Problem
case statement goes directly into my default statement, which issues an invalid information message to the user.
My Progress
This will be my complete program, everything seems to work properly except that my case statement recognizes every month as invalid input
// Import Libraries
import javax.swing.*;
import java.util.*;
import java.io.*;
// This is my program.
public class DateCalc
{
public static void main (String[] args)
{
String month;
String day;
String inputYear;
Scanner keyboard = new Scanner(System.in);
// receiving input for my age variable
System.out.print( "Please Enter The Month Of The Date :");
month = keyboard.nextLine();
// receiving input for my weight variable
System.out.print( "Please Enter The Day Of The Date : ");
day = keyboard.nextLine();
// receiving input for my height variable
System.out.print( "Please Enter The Year OF The Date : ");
inputYear = keyboard.nextLine();
String stringYear = ""+ inputYear.charAt(inputYear.length()-2) + inputYear.charAt(inputYear.length()-1);
int year = Integer.parseInt(stringYear);
int intDay = Integer.parseInt(day);
switch(month)
{
// I tried to test my program by using my first case " January ", However it goes right through every case directly for my default case.
case "January || january" :
int janKey = 1;
int janQuarter = year / 4;
int janSum = year + janQuarter + intDay + janKey;
System.out.print( " Date Entered Was : " + month + ","+ day + "" + inputYear);
System.out.print( " Last Two Digits Of The Year Were : " + year);
System.out.print( " One Quarter Of Last Two Digits : " + janQuarter);
System.out.print( " The Given Day Of The Month Entered : " + day);
System.out.print( " The Index Key This Moth is : " + janKey);
System.out.print( " The Sum Of All The number Above is : " + janSum);
System.out.print( " \n \n The Day Of The Week Was : ");
int weekDay = dayLookUp(janSum);
System.out.print( " \n \n The Day Of The Week Was : " + weekDay);
break;
case "February || february":
int febKey = 4;
break;
case "March || march":
int marKey = 4;
break;
case "April || april":
int aprKey = 0;
break;
case "May || may":
int maykey = 2;
break;
case "June || june":
int junKey = 5;
break;
case "July || july":
int julKey = 0;
break;
case "August || august":
int augKey = 3;
break;
case "September || september":
int septKey = 6;
break;
case "October || october":
int octKey = 1;
break;
case "November || november":
int novKey = 4;
break;
case "December || december":
int decKey = 4;
break;
// IN MY DEFUALT CASE " inputValidation " WILL BE EXECUTED
default:
JOptionPane.showMessageDialog(null," Invalid Entry Please Try Again " );
}
}
public static int dayLookUp ( int janSum )
{
int sum = janSum;
int day = 14 % 7;
return day;
}
}
The way you're doing it now, it's looking for the whole string as is, literally, not interpreting the || as any form of or.
You can either set the element in the switch to uppercase or lowercase use :
switch(month.toLowerCase()) {
case "january" :
...
break;
case "february":
...
...
}
or you have to double case elements:
switch (month) {
case "January":
case "january":
...
break;
case "February":
case "february":
...
...
}
The mistake is at "January || january" this is one String use case "January": case "january":
You cannot test for alternatives this way, case "January || january": doesn't work. You can give alternatives with multiple cases
switch (month) {
case "january":
case "January":
int janKey = 1;
without an intervening break. This causes a fall through to the second case, when january is entered. The same is with the other months, of course.
I think it might be due the switch statement.
Do I have make the both them switch statements for them to work out.
import java.util.Scanner;
public class mylab
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int month;
int day;
String season= "seasons";
System.out.print("type a two digit month");
System.out.print(" and day");
month = in.nextInt();
day = in.nextInt();
String winter = " winter ";
String summer = " summer";
String spring = " spring";
System.out.print(" Month="+ month +" Day= "+day);
switch (month) {
case 1:
month = 1; System.out.println(" Winter");
break;
case 2:
month = 2; System.out.println(" Winter");
break;
case 3:
month= 3;System.out.println(" Winter");
break;
case 4:
month= 4;System.out.println(" Spring");
break;
case 5:
month = 5;System.out.println(" Spring");
break;
case 6:
month = 6 ;System.out.println(" Spring");
break;
case 7:
month = 7 ;System.out.println(" Summer");
break;
case 8:
month = 8;System.out.println(" Summer");
break;
case 9:
month = 9;System.out.println(" Summer");
break;
case 10:
month = 10;System.out.println(" Fall");
break;
case 11:
month = 11;System.out.println(" Fall");
break;
case 12:
month = 12;System.out.println(" Fall");
break;
}
How , do I make this part work with the switch statement
the pseudo code for this portion is If month is divisible by 3 and day >= 21, If season is "Winter", season = "Spring",Else if season is "Spring", season = "Summer",Else if season is "Summer", season = "Fall"
Else season = "Winter"
if (month % 3 == 0 && day >= 21)
{
if ( season.equals(winter) )
System.out.println(" Spring");
else if ( season.equals(spring) )
System.out.println ( "Summer" );
else if ( season.equals(summer) )
System.out.println ( " fall");
else if ( season.equals(winter) )
System.out.println( " winter");
}
}
}
This is how I'd probably write it (if I absolutely had to keep the switch and I didn't care to check user input):
import java.util.Scanner;
public class mylab {
public static void main(String[] args) {
int month, day;
Scanner in = new Scanner(System.in);
System.out.print("Type a two digit month: ");
month = in.nextInt();
System.out.print("Type a two digit day: ");
day = in.nextInt();
System.out.print(" Month="+ month +" Day= "+day+" ");
if(month%3==0 && day>=21) {
month++;
if(month>12) month=1;
}
switch (month) {
case 1: case 2: case 3: System.out.println("Winter"); break;
case 4: case 5: case 6: System.out.println("Spring"); break;
case 7: case 8: case 9: System.out.println("Summer"); break;
case 10: case 11: case 12: System.out.println("Fall"); break;
}
}
}