• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about removing a single specific character on the last output

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am having a little bit of trouble with my code I am almost at the end yet I have a small problem, is there a way to remove the last "-" of the output of this code? for example I input "hello" the output I am for should be "15-12-12-5-8" and not "15-12-12-5-8-" I tried using str.substring(0,str.length()-1); but in the end it removed all of the dash and I only want the last dash of the code to be removed, I hope anyone can help me.



 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your problem is here:

your loop says "print each character, followed by a dash"

but it seems like you don't want to do that...you only want to print a dash if there is another character left to print.  So, you need some kind of condition around the printing of the dash.  Can you think about a way to determine if there is another character still?
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a naughty trick to get rid of the last −; I have mentioned it recently in a different context.
Find out about a StringJoiner object, which can produce exactly the output you want and does all the testing for you.
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic