Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to add the amount in between a string based on the number of input amounts with delimiter ,

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trying to write code which needs to add delimiter , and also use 'and' for the last amount.
my string will vary depends on the input.
ex:
One amount:
• "Your amount of $DD.CC is going to added to your account within three days."

Multiple amounts:
• “Your amounts of $DD.CC and $DD.CC are going to added to your account within three days."
• “Your amounts of $DD.CC, $DD.CC, and $DD.CC are going to added to your account within three days."
• “Your amounts of $DD.CC, $DD.CC, $DD.CC, and $DD.CC are going to added to your account within three days."

Multiple amounts may increase.


I used switch/case:

Can you provide me a good code.

Thanks.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that dollars and cents are all different that probably means putting them in an array.

Have you ever written pseudo code? It's sort of half English and half code. It is useful for describing an algorithm without getting down to specific code syntax.


 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


please guide to write this code more efficiently.

Thanks.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a loop at all?


Instead of using "+" to concatenate string constants, do it in code. As in:

Have you tried to compile and run this? What did you get?
 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I tried to run it..it is working fine...just want to make sure the code I wrote is efficient one...I will update the code as you suggested and test it..got curious on another one about default block...

default:

               for(int i=0;i<length;i++){

                   if(i==length-1){
                       amt.append( "and"+" "+"$"+amounts[i]);
                       msg.append( str1 + amt + str2);
                       break;
                   }
                   amt.append( "$"+amounts[i]+", ");
               }


How to use other way of for loop on this block?
"for(int i : amounts)"
how to make a if check here  if I use this "for loop"?

Please let me know
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay..I will use the normal standard for loop. Got another question...about StringBuilder..which way of using is performance efficient of these examples.

eg:
msg.append("Your amount of  " + " " +"$" + amounts[0]+" "+"is going to added to your account within three days.");

msg.append("Your amounts of ").append(" ").append("$").append(amounts[0]).append(" ").append("is going to added to your account within three days.");

Can you explain me which way is good to use? thanks.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem with this is that you actually have two StringBuilder objects, the 'msg' object that you explicitly create, and an implicit one added by the compiler to handle the "+" operations. Why not just have 'msg' then?
 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String beginningString = "Your amounts of ", endingString = " are going to added to your account within three days.;
StringBuilder amount = new StringBuilder();
StringBuilder message = new StringBuilder();
if(CollectionUtils.isNotEmpty(list)){
int size = list.size();
switch (size){
case 1:
message.append("Your amount of $").append(list.get(0)).append(" is pending processing and will post to your account within one to two business days.");
break;
case 2:
message.append(beginningString).append(("$"+list.get(0)+ " and $" + list.get(1))).append(endingString);
break;
default:
for (int i = 0; i < size; i++) {
if (i == size - 1) {
amount.append("and $").append(list.get(i));
message.append(beginningString).append(amount).append(endingString);
break;
}
amount.append("$"+list.get(i) + ", ");
}
}
}

I fixed most of them...can you verify whether it need any more update to make the code more efficient?
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to indent your code consistently. Other than that it seems good. Have you tested it?
 
Adi Ishi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I tested it, it is working fine.I indent the code in project...sorry didn't copy it here properly... thanks for your help. I appreciate it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!