Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Switch case for months names vs digits

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, guys!

A user enters months into text field using numbers, e.g. number 5 is entered, and it should be converted to String "May".
I used the following solution. I don't know, it doesn't feel right. Is there a better way?


 
Marshal
Posts: 76476
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the fact that there are already many classes designed for that sort of thing: see the Java™ Tutorials....
Try this sort of thing:-
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's work

public void howMonth() throws IOException {
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       String numMonth=reader.readLine();
       switch (numMonth){
           case "1":
               System.out.println("Jan");
               break;
           case "2":
               System.out.println("Feb");
               break;
           case "3":
               System.out.println("Mar");
               break;
           case "4":
               System.out.println("Apr");
               break;
           case "5":
               System.out.println("May");
               break;
           case "6":
               System.out.println("Jun");
               break;
           case "7":
               System.out.println("Jul");
               break;
           case "8":
               System.out.println("Aug");
               break;
           case "9":
               System.out.println("Sep");
               break;
           case "10":
               System.out.println("Oct");
               break;
           case "11":
               System.out.println("Nov");
               break;
           case "12":
               System.out.println("Dec");
               break;
           default:
               System.out.println("Error! Non month!");
               break;
       }
   }
}
 
Sheriff
Posts: 17147
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

oroszCapa Blaszczekievicz wrote:I don't know, it doesn't feel right. Is there a better way?


Campbell showed a better way.

The feeling you had about it not being right is a good sign in the sense that your intuition told you something was off with it. Maybe you thought it was way too long or that it seemed repetitious. You'd be right about that.

Java 13 introduced a preview feature called "switch expressions" which are disabled by default. If you try this in JShell, you have to start it with the --enable-preview option.


It's still long though, and tedious to type. Still not as good as a lookup table (the method Campbell showed earlier)
 
Bartender
Posts: 5074
189
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the API of the Month enum. It can be done in just one line of code.
 
Marshal
Posts: 8429
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Still not as good as a lookup table (the method Campbell showed earlier)


As far as I know, switch statement is implemented to use loookupswitch or tableswitch operations, which essentially use lookup tables. Which operation in particular to use is decided based on the present cases within the switch statements's block (one can do more research for more details). So from the performance standpoint, lookup time complexity supposed to be as as fast as in arrays if that was one of concerns/reasons to say that switch statement "...isn't as good as lookup table", but knowing Junilu, I presume he addressed more simplicity and clarity of the solution rather than performance.

I actually perhaps would prefer approach with switch over Campbell's proposed arrays solution in OP's given scenario, because it seems OP as a month translation keys have strings, which is easier to handle in switch statement, than to use some parsing constructs and then handle potential exceptions (i.e. ArrayIndexOutOfBoundsException, NumberFormatException). Of course that is doable, and having that Campbell's proposed solution would become quite an elegant.

Now I just saw Piet's suggested yet another approach - that is by far most elegant it seems, just need to handle same aspects of parsing as in Campbell's suggestion.

Depending of course on OP's requirements, whether it is something for college with prescribed instructions what is allowed or what is not, or whether it is something "free to choose", Piet's solution looks very promising.
 
oroszCapa Blaszczekievicz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the input!
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic