Okay so i'm trying to complete this project for school and it is asking me to make a switch statement of the months. So if a user enters 1 it will print out January and so on... I get this error where it says: cannot find symbol - variable January
import java.util.*;
/**
* Outputs the number on a month name entered by the user.
*
* #author Jack
* #version 1a
*/
public class MonthSwitchReverse {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int monthString;
String month;
System.out.println(" Jan = 1 / Feb = 2 / March = 3 / April = 4 / MAy = 5 / June = 6");
System.out.println(" July = 7 / Aug = 8 / Sep = 9 / Oct = 10 / Nov = 11 / Dec = 12");
System.out.print("Choose a month above and the system will print out the number assigned to that month: ");
month = in.next();
switch (month) {
case "January": monthString = 1;
break;
case "February": monthString = 2;
break;
case "March": monthString = 3;
break;
case "April": monthString = 4;
break;
case "May": monthString = 5;
break;
case "June": monthString = 6;
break;
case "July": monthString = 7;
break;
case "August": monthString = 8;
break;
case "September": monthString = 9;
break;
case "October": monthString = 10;
break;
case "November": monthString = 11;
break;
case "December": monthString = 12;
break;
default: monthString = 404;
break;
}
System.out.println(monthString);
}
}
EDIT: I fixed it. Thanks for your help.
Since you are passing an integer to switch , That each case should be an int value.
For example
case 1: monthString = "1";
case 2: monthString = "2";
And same for remaining all.
recommending to read : Switch in java.
Related
This question already has answers here:
Why can't I initialize a variable inside a switch in Java?
(6 answers)
Closed 2 years ago.
This code is for the purpose of changing months to the corresponding letter codes.
Here is my code I wrote so far:
public static void main (String[ ] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter month number. [1..12] --> ");
int month = input.nextInt();
String MonthString;
switch (month)
{
case 1 : MonthString = "ZS"; break;
case 2 : MonthString = "CN"; break;
case 3 : MonthString = "YH"; break;
case 4 : MonthString = "MT"; break;
case 5 : MonthString = "CL"; break;
case 6 : MonthString = "SS"; break;
case 7 : MonthString = "WM"; break;
case 8 : MonthString = "WY"; break;
case 9 : MonthString = "SH"; break;
case 10 : MonthString = "YJ"; break;
case 11 : MonthString = "XG"; break;
case 12 : MonthString = "HZ"; break;
default : System.out.print("This is not a valid month number.");
}
System.out.println(MonthString);/*This is where it won't compile*/
}
You need to initialize MonthString first:
String MonthString = null;
default: MonthString = "This is not a valid number";
you need to assign the value in the default of your switch statement, not print it out since it's going to get printed after the switch/case is over.
You need to put something on String monthString = ""; first.
Note lowercase and uppercase letters in a variable boot :
When you initialize a variable it should be initialized as follows:
String firstExample= "Hello world"; and not : String FirstExample= "Hello world"; , Classes are given names with capital letters.
public static void main (String[ ] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter month number. [1..12] --> ");
int month = input.nextInt();
String monthString = "";
switch (month)
{
case 1 : monthString = "ZS"; break;
case 2 : monthString = "CN"; break;
case 3 : monthString = "YH"; break;
case 4 : monthString = "MT"; break;
case 5 : monthString = "CL"; break;
case 6 : monthString = "SS"; break;
case 7 : monthString = "WM"; break;
case 8 : monthString = "WY"; break;
case 9 : monthString = "SH"; break;
case 10 : monthString = "YJ"; break;
case 11 : monthString = "XG"; break;
case 12 : monthString = "HZ"; break;
default : System.out.print("This is not a valid month number.");
}
System.out.println(monthString);
}
}
Output :
Enter month number. [1..12] --> 1
ZS
import java.util.Scanner;
public class DaysInMonth
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a year:")
int year = input.nextInt(); enter code here
System.out.print("Enter a month:");
int month = input.nextInt(); enter code here
int days = 0;
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);
switch (month){
case 1:
days = 31;
break;
case 2:
if (isLeapYear)
days = 29;
else
days = 28;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
default:
String response = "Have a Look at what you've done and try again";
System.out.println(response);
System.exit(0);
}
String response = "There are " +days+ " Days in Month "+month+ " of Year " +year+ ".\n";
System.out.println(response); // new line to show the result to the screen.
}
}
Why can't I type in January to get the same output result if I type 1? It should print "There are 31 days in the month of January of Year 2018" I initialized the month so it should read January or any other month.
I know I have int but I am wondering how can I also use January for 1 to get the same output.
You can use Strings in a switch statement to check for several equivalent cases:
switch (monthInput.toLowerCase()) {
case "january":
case "jan":
case "1":
days = 31;
break;
case "february":
case "feb":
case "2":
days = isLeapYear ? 29 : 28;
break;
case "march":
case "mar":
case "3":
days = 31;
break;
// etc.
default:
System.out.println(monthInput + " is not a valid month");
input.close();
System.exit(0);
}
But this means you have to read your input as a String, not as an int...
Scanner input = new Scanner(System.in);
System.out.print("Enter a year:");
int year = input.nextInt(); // enter code here
input.nextLine(); // read the rest of the line (if any)
System.out.print("Enter a month:");
String monthInput = input.nextLine();
Note the use of input.nextLine(); after the .nextInt() — this is because the nextInt() call does not consume all of the input, it only reads the int that you typed for the Year, it does not read the newline (enter key), so you have to read that to be ready to read the next input, which is the month number or name.
I know I have int but I am wondering how can I also use January for 1 to get the same output.
A simple approach is to use name array
// before main.
static final String[] MONTH = "?,Jan,Feb,Mar,Apr,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(",");
// inside main
String monthStr = MONTH[month];
Add the full month names as needed.
I need to know which day and day name is on a specific date. And somehow I made a mistake because for years with 19.. it works but at 2000 I can't get the right day anymore. For example if I use the date 9.1.2001 it doesn't say me the day, instead the error of the switch I made occurs.
and i shouldn't use the calendar methods
here is my code:
public class FindDay {
public static void main(String[] args) {
System.out.println("Type a day: ");
int day = In.readInt();
System.out.println("Type a month: ");
int month = In.readInt();
System.out.println("Type a year: ");
int year = In.readInt();
if(day < 1 || day > 32) {
System.out.println("Type valid day");
}else if (month < 1 || month >12) {
System.out.println("Type valid month");
}else if (year < 1900) {
System.out.println("Type valid year");
}
int wholeDaysYear; //to calculate how much days a year has
if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ) {
wholeDaysYear = 366;
}else {
wholeDaysYear = 365;
}
int monthDays = 0; //calculates days to this month
int february; //if February has 28 or 2 days
if(wholeDaysYear ==366) {
february = 29;
}else {
february = 28;
}
switch(month) {
case 1: monthDays= 0; break;
case 2: monthDays =31; break;
case 3: monthDays= 31+february; break;
case 4: monthDays= 31+february+31; break;
case 5: monthDays= 31+february+31+30; break;
case 6: monthDays= 31+february+31+30+31; break;
case 7: monthDays= 31+february+31+30+31+30; break;
case 8: monthDays= 31+february+31+30+31+30+31; break;
case 9: monthDays= 31+february+31+30+31+30+31+31; break;
case 10: monthDays= 31+february+31+30+31+30+31+31+30; break;
case 11: monthDays= 31+february+31+30+31+30+31+31+30+31; break;
case 12: monthDays= 31+february+31+30+31+30+31+31+30+31+30; break;
default: System.out.println("Enter valid month!");break;
}
int leapYear = ((year-1) / 4 - 474) - ((year-1) / 100 - 18) + ((year-1) / 400 - 4); //calculates the leap years
int allDays =(((year - leapYear)-1900)*wholeDaysYear)+monthDays+day-1; //Calculates all days
System.out.println(allDays);
int dayName = allDays % 7;
String dayNames = null; //gives the days their name
switch (dayName) {
case 1: dayNames = "Monday";break;
case 2: dayNames = "Tuesday";break;
case 3: dayNames = "Wendesday";break;
case 4: dayNames = "Thursday";break;
case 5: dayNames = "Friday";break;
case 6: dayNames = "Saturday";break;
case 7: dayNames = "Sunday";break;
default: System.out.println("Type valid Input");break;
}
System.out.println(dayNames);
}
}
You don't have to work so hard... assuming you have the day of the month, month of the year and full-year, you can simply do (example):
LocalDate ld = LocalDate.of(1999, 12, 12);
System.out.println(ld.getDayOfWeek()); // prints SUNDAY
LocalDate is available since Java 8.
As for your code, two bugs that are easy to spot are:
The cases in the switch go from 1 to 7 while they should go from 0 to 6 since you're calculating the reminder out of 7.
int dayName = allDays % 7; here you assume that the first day of the year is Monday (according to your case switch) which is not necessarily true.
You can try something like this:
Date yourDate;
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH); //or Calendar.DAY_OF_WEEK
// etc.
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;
}
}
}
What's wrong with my code? For some reason I keep on getting the day off by one day? For example, today is the 26th of 2013 and it's a Tuesday, but the program tells me it's a Wednesday. I am using Zeller's Congruence algorithm.
import javax.swing.JOptionPane;
public class zeller {
public static void main(String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString = JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 13: monthString = "January";
break;
case 14: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100 ;
double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +(j / 4) + (5 * j)) % 7;
int h1 = (int)h;
switch (h1) {
case 0: dayString = "Saturday";
break;
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
default: monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}
The months Jan and Feb are counted as the 13th and 14th month of the previous year, so
if you want the user to enter the actual months of 1 or 2 for Jan or Feb you could add code like this:
if (month == 1 ){
month = 13;
year -=1;
}
else if (month == 2) {
month = 14;
year -=1;
}
You need to modify the year if it is January or February. Explanation on wikipedia:
One can readily see that, in a given year, March 1 (if that is a
Saturday, then March 2) is a good test date; and that, in a given
century, the best test year is that which is a multiple of 100. Zeller
used decimal arithmetic, and found it convenient to use J and K in
representing the year. But when using a computer, it is simpler to
handle the modified year Y, which is Y - 1 during January and
February:
I modified your code as follows and it works:
import javax.swing.JOptionPane;
public class zeller {
public static void main(final String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString =
JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 14:
year--;
monthString = "January";
break;
case 13:
year--;
monthString = "February";
break;
case 3:
monthString = "March";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100;
double h = (day + ((13 * (month + 1) / 5)) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
int h1 = (int) h;
switch (h1) {
case 0:
dayString = "Saturday";
break;
case 1:
dayString = "Sunday";
break;
case 2:
dayString = "Monday";
break;
case 3:
dayString = "Tuesday";
break;
case 4:
dayString = "Wednesday";
break;
case 5:
dayString = "Thursday";
break;
case 6:
dayString = "Friday";
break;
default:
monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}
From Wikipedia,
In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is February 2, 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format).
So there is technically nothing wrong with your code; putting in 02/26/2013 (today) as the 26th day of the 14th month of 2013 is actually calculating the day of the week of 02/26/2014.