The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help in optimisation.

1 reply on 1 page. Most recent reply: Jun 18, 2003 2:39 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
kapil

Posts: 1
Nickname: kapil
Registered: Jun, 2003

Need help in optimisation. Posted: Jun 17, 2003 9:59 PM
Reply to this message Reply
Advertisement
In my work assignment , one of the java class returns the string messages according to the inetger number which been given as input to its method. There are around 300 string messages right now inside that class. The integer value varies from range 100 to 200, 200 to 300, 300 to 400, 400-500 and it's goes on. The Class has one method getMesg(int number) inwhich I have coded as below.
Is there any other way to implement the same functionality with performance improvement??

String strMesg="";
if ((number > 100) && (number < 200))
{
switch(number)
{
case 101: strMesg = "String Message 101";
break;
case 102: strMesg = "String Message 102";
break;
............
............
}
}
else if ((number > 200) && (number < 300))
{
switch(number)
{
case 201: strMesg = "String Message 201";
break;
case 202: strMesg = "String Message 202";
break;
............
............
}
}
else if((number > 300) && (number < 400))
{
switch(number)
{
case 301: strMesg = "String Message 301";
break;
case 302: strMesg = "String Message 302";
break;
............
............
}
}


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Need help in optimisation. Posted: Jun 18, 2003 2:39 AM
Reply to this message Reply
For a start there is no need (that I can see) for the if-statements, or for the else-if-statements. The case statement on its own should be sufficient.

Have a look at the following link

http://java.sun.com/docs/books/tutorial/i18n/

for details on Internationalization. This should provide a handy technique where your strings are stored in a text file and easily extracted using a key-value technique where your key is your number 100, 200, etc and your value is the string you wish to return. This will allow you to change your messages without having to recompile the code.

Of particular interest in the tutorial mentioned above would be the Isolating Locale-Specific Data, Formatting and Working with Text sections.

Adam

Flat View: This topic has 1 reply on 1 page
Topic: Implement Collection toArray Previous Topic   Next Topic Topic: How much testing is too much testing?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use