• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

Enum alternative for java 1.4 Compiler

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
matrixrasta
Asked:
matrixrasta
  • 9
  • 8
  • 5
  • +1
2 Solutions
 
ksivananthCommented:
why don't you compile this using 1.5 compiler for version 1.4?
0
 
ksivananthCommented:
for e.g., javac -version 1.4...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CEHJCommented:
You might like to use the Calendar constants Calendar.JANUARY, Calendar.FEBRUARY etc but of course these are not type-safe
0
 
matrixrastaAuthor Commented:
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
 
CEHJCommented:
>>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
 
matrixrastaAuthor Commented:
Sorry that's what I meant characters to numeric values
0
 
CEHJCommented:
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
 
matrixrastaAuthor Commented:
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
 
objectsCommented:
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
 
CEHJCommented:
>>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
 
objectsCommented:
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
 
CEHJCommented:
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
 
matrixrastaAuthor Commented:
That great but it only shows a single digit it should show double e.g. Sep = 09  it shouldn't just show (9)
0
 
CEHJCommented:
Did you run the code i posted?
0
 
objectsCommented:
>    return index==-1 ? null : Integer.toString((index/3) + 1);

change that to

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

0
 
matrixrastaAuthor Commented:
yep i did
0
 
CEHJCommented:
>>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
 
matrixrastaAuthor Commented:
>> return index==-1 ? null : String.format("%02d", (index/3) + 1);
This is the java codes error Objects
error.jpg
0
 
matrixrastaAuthor Commented:
Sorry if thats not to clear
it shows the String.format is not an applicable argument.
0
 
objectsCommented:
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
 
CEHJCommented:
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
 
matrixrastaAuthor Commented:
Both answers work perfectly.
Thanks you guys!
0
 
CEHJCommented:
:-)

Be careful - your hard-coded string is wrong (for German anyway)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 9
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now