Click here to Skip to main content
15,888,070 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Scanner sc = new Scanner(System.in);
String month = sc. nextLine();
switch (month){
case "January":
System.out.println("Ye");
break;
case " February" : 
System.out.println("Hello");
break;
case "May":
System.out.println("nice");
break;
case "October":
System.out.println("ll");
break;
case "November":
System.out.println("o");
break;
case "April":
System.out.println("H");
break;
case "March":
System.out.println("He");
break;
case "December":
System.out.println("Hell");
break;
default:
System.out.println("Invalid");
}
if (....?


What I have tried:

I want that if the months are matching, it should print the statements associated with them as they are already doing. And if anyone of the months are matched, it says something like "want to know more?"

And if nothing matches, along with invalid, it should say nothing. But I don't want to add this in every case. I want to do it using if else statement. Also how can use January, JANUARY and january at the same time?
Posted
Updated 27-Jul-23 20:39pm
v3

You already have the default, which will print "Invalid" if the input does not match any of the other cases. If you want to print something extra then try:
Java
String found = "Success"
switch (month) {
// cases as above
default:
    System.out.println("Invalid");
    found = "Failure";
}
System.out.println(found);
 
Share this answer
 
Comments
[no name] 5-Aug-23 20:33pm    
System.in
Add a boolean: call it MatchedOK and set it to true just above the switch.
In your default case, set it to false.

You can then test MatchedOK and tell which way the code went.

By the way ... You should probably use toLowerCase on the switch variable and use all lowercase strings in your case statements: that way it will match "JANUARY", "January", "january", and all other upper and lower case combinations.
 
Share this answer
 
v2
Comments
Rudra Jul2023 22-Jul-23 9:34am    
Can you please provide the code
OriginalGriff 22-Jul-23 9:52am    
Which bit is giving you difficulties?
Rudra Jul2023 22-Jul-23 12:39pm    
The one you added-
You should probably use toLower on the switch variable and use all lowercase strings in your case statements: that way it will match "JANUARY", "January", "january", and all other upper and lower case combinations.
I am sorry but I didn't understood what you want to say - can you please provide the code?
OriginalGriff 22-Jul-23 12:55pm    
You are kidding, right?
If you can't write this line of code yourself, then you need to go right back to the beginning of learning the language and start again:
switch (month.toLowerCase()){
[no name] 5-Aug-23 20:33pm    
Thank you
Another problem you have:
Java
case " February" :
System.out.println("Hello");
break;

Replace " February" with "February" , the space matters.
 
Share this answer
 
I'd rather use a map...
(and follow Griff's advice on lower case)
Java
import java.util.*;
  
public class Year
{

  public static void main( String args[])
  {
    Map<String,String> mAns = new HashMap<String,String>();
    mAns.put("january", "Ye");
    mAns.put("february", "Hello");
    mAns.put("march", "He");
    mAns.put("april", "H");
    mAns.put("may", "Nice");
    mAns.put("june", "Very nice");
    mAns.put("july", "Well");
    mAns.put("august", "Ah");
    mAns.put("september", "See");
    mAns.put("october", "Oh");
    mAns.put("november", "No");
    mAns.put("december", "Doh");

    Scanner scanner = new Scanner(System.in);
    while (true)
    {
      System.out.println("choose your month 'quit' to terminate:");
      String entry = scanner.nextLine();
      entry = entry.toLowerCase();
      if ( entry.equals("quit") ) break;
      System.out.println(answerTo( entry, mAns));
    }
    System.out.println("goodbye");
  }

  private static String answerTo( String entry, Map<String, String > ma)
  {
    String answer = "invalid entry";
    if ( ma.containsKey( entry ))
    {
      answer = ma.get(entry);
    }
    return answer;
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900