Reputation: 19
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;
}
}
Upvotes: 0
Views: 824
Reputation: 74018
You cannot test for alternatives this way, case "January || january":
doesn't work. You can give alternatives with multiple case
s
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.
Upvotes: 1
Reputation: 16628
The mistake is at "January || january"
this is one String
use case "January": case "january":
Upvotes: 1
Reputation: 6437
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":
...
...
}
Upvotes: 6