This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you done so far? Where exactly are you stuck?
 
Carey Brown
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


please guide to write this code more efficiently.

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you found out, enhanced for() loops don't help in your situation.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't verify it without CodeTags. Please re-post using CodeTags.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to indent your code consistently. Other than that it seems good. Have you tested it?
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Carey Brown
Saloon Keeper
Posts: 8941
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
reply
    Bookmark Topic Watch Topic
  • New Topic