Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enum alternative for java 1.4 Compiler

Posted on 2009-04-15
24
Medium Priority
?
1,056 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

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!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

721 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