Improve company productivity with a Business Account.Sign Up

x
?
Solved

Enum alternative for java 1.4 Compiler

Posted on 2009-04-15
24
Medium Priority
?
1,106 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 1000 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 1000 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

606 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