• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

about removing a single specific character on the last output

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 12954
65
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 71629
312
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic