Solved

Enum alternative for java 1.4 Compiler

Posted on 2009-04-15
24
1,031 Views
Last Modified: 2013-12-15
Dear Experts
I need an alternative for below code that is compatible to a 1.4 compiler, because the 1.4 compiler doesn't support the enum


enum MonthRef{
      Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
}
      
public String getDayNo(String day) {
      //use case
      MonthRef ref = null ;
         
      try{
          ref = MonthRef.valueOf( day ) ;
      }catch( IllegalArgumentException iae ){
           System.out.println( "Invalid option: " + day ) ;
           return null ;
      }
                
      switch( ref ){
      case Jan : return "01" ;
            case Feb : return "02" ;
            case Mar : return "03" ;
            case Apr : return "04" ;
            case May : return "05" ;
            case Jun : return "06" ;
            case Jul : return "07" ;
            case Aug : return "08" ;
            case Sep : return "09" ;
            case Oct : return "10" ;
            case Nov : return "11" ;
            case Dec : return "12" ;
       }
       return null ;
}
0
Comment
Question by:matrixrasta
  • 9
  • 8
  • 5
  • +1
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24146273
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24146285
why don't you compile this using 1.5 compiler for version 1.4?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24146291
for e.g., javac -version 1.4...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 24146358
You might like to use the Calendar constants Calendar.JANUARY, Calendar.FEBRUARY etc but of course these are not type-safe
0
 

Author Comment

by:matrixrasta
ID: 24146726
I'm sorry but that doesn't help my situation.
The server at which the java file will compile only supports 1.4

And the java code has to change the monthly numeric value to the 3 characters represented for Oracle database
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24147053
>>And the java code has to change the monthly numeric value to the 3 characters represented for Oracle database

I'm confused. That's not what the code you posted is doing. It's actually doing the reverse
0
 

Author Comment

by:matrixrasta
ID: 24147361
Sorry that's what I meant characters to numeric values
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24148015
Just use the resources i mentioned and forget enum:
import java.text.*;
public class Month {
    private  static String[] MONTHS = new DateFormatSymbols().getShortMonths();
    private static DecimalFormat df = new DecimalFormat("00");
 
    public static void main(String[] args) {
	// For testing
	String s = Month.getMonth(args[0]);
	System.out.println(s);
    }
 
    public static String getMonth(String shortName) {
	String result = null;
	for(int i = 0;i < MONTHS.length && result == null;i++) {
	    if (MONTHS[i].equals(shortName)) {
		result = df.format(i + 1);
	    }
	}
	return result;
    }
}

Open in new window

0
 

Author Comment

by:matrixrasta
ID: 24241399
Sorry mate! I decided to keep solve it as simple as possible

public String getDayNo(String day) {
                
      if (day.equals("Jan")){
            return "01";
      }else if(day.equals("Feb")){
            return "02";
      }else if(day.equals("Mrz")){
            return "03";
      }else if(day.equals("Apr")){
            return "04";
      }else if(day.equals("Mai")){
            return "05";
      }else if(day.equals("Jun")){
            return "06";
      }else if(day.equals("Jul")){
            return "07";
      }else if(day.equals("Aug")){
            return "08";
      }else if(day.equals("Sep")){
            return "09";
      }else if(day.equals("Okt")){
            return "10";
      }else if(day.equals("Nov")){
            return "11";
      }else if(day.equals("Dec")){
            return "12";
      }else{
      return null ;
      }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 24246385
you can simplify that using the following:

public String getDayNo(String day) {
   String months = "JanFebMrzAprMaiJunJulAugSepOktNovDec";
   int index = months.indexOf(day);
   return index==-1 ? null : Integer.toString((index/3) + 1);
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24248177
>>String months = "JanFebMrzAprMaiJunJulAugSepOktNovDec";

Be careful with that - it won't be locale-sensitive, so may give incorrect results depending on where you run it. It's currently not quite right, even for a German locale btw. See below for locale-sensitivity
    public static String getMonthNo(String month) {
        String result = null;
        String[] months = new java.text.DateFormatSymbols().getShortMonths();
        for(int i = 0;result == null && i < months.length;i++) {
            if (months[i].equals(month)) {
                result = String.format("%02d", i + 1); 
            }
        }
        return result;
    }   

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24248273
thats completely irrelevant, and overcomplicating things (again) for no reason.
not to mention that it won't actually work reliably.
always keep your code simple, saves you time in the long run.


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24248294
matrixrasta, in case you're confused by that last comment, Java has been designed from the outset to be internationalised. That is a great strength, but occasionally can also be a problem. Let me know if you need more input on the issue, but it would be better if you ran both pieces of code first
0
 

Author Comment

by:matrixrasta
ID: 24249418
That great but it only shows a single digit it should show double e.g. Sep = 09  it shouldn't just show (9)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24249455
Did you run the code i posted?
0
 
LVL 92

Expert Comment

by:objects
ID: 24249459
>    return index==-1 ? null : Integer.toString((index/3) + 1);

change that to

   return index==-1 ? null : String.format("%02d", (index/3) + 1);

0
 

Author Comment

by:matrixrasta
ID: 24249471
yep i did
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24249495
>>yep i did

You'll see it prints the correct number of digits, the code which does so, having just been copied. If you want to fix the Locale for some reason, you can do

new DateFormatSymbols(Locale.GERMAN);
0
 

Author Comment

by:matrixrasta
ID: 24249500
>> return index==-1 ? null : String.format("%02d", (index/3) + 1);
This is the java codes error Objects
error.jpg
0
 

Author Comment

by:matrixrasta
ID: 24249522
Sorry if thats not to clear
it shows the String.format is not an applicable argument.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24249609
thats weird, compiles fine here.

this will also do the job

 int month = (index/3) + 1;
 return index==-1 ? null : (month<10 ? "0" + month : Integer.toString(month));

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24249744
Forgot you were needing 1.4, so
    public static String getMonthNo(String month) {
        String result = null;
        java.text.DecimalFormat df = new java.text.DecimalFormat("00");
        String[] months = new java.text.DateFormatSymbols().getShortMonths();
        for(int i = 0;result == null && i < months.length;i++) {
            if (months[i].equals(month)) {
                result = df.format(i + 1); 
            }
        }
        return result;
    }   

Open in new window

0
 

Author Comment

by:matrixrasta
ID: 24251924
Both answers work perfectly.
Thanks you guys!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24252036
:-)

Be careful - your hard-coded string is wrong (for German anyway)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 11 59
tomcat administrtor 12 68
type mismatch (Object[] to double[] 4 41
wild fly 8 startup error 2 42
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question