Solved

Enum alternative for java 1.4 Compiler

Posted on 2009-04-15
24
1,021 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 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