• 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

Changing filename

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a file name 1234_abc_abcd_45678_01-05-2010.rtf which I need to remove everything before the 3rd underscore. This is probably easy but I am trying to teach myself Java and don't come from a programming background. Thanks for the help.


var filename = "1234_abc_abcd_45678_01-05-2010.rtf";

for (int i = 0; i < fileName.length(); i++)
{
if (fileName.charAt(i) == '_')
{
counter++;
}
}

if (counter==3)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's close, but you don't keep a reference to exactly where the third underscore is. Also, you can break out of the loop after the third one.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as a general tip, before you write any code, write down in english all the steps. yours would be something like

then you look at it, and break down each step further. HOW do you find the third underscore?


ok...so you look at this...is this right? sort of...it finds the third underscore, bu then blows right on past it... let's revise it again...

once you get to a point where it's clear how to code each part, you start writing code. Then, write as little code as possible before you compile, debug and test.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch!

I would tell you exactly what fred said above. Follow that advice!
 
Chris Sanders
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.


I think I found an easier way to do what I am trying to accomplish with the following.

var fileName = "1234_abc_abcd_45678_01-05-2010.rtf";

var filePieces = fileName.split("_");

var newFileName = filePieces[3] + "_" + filePieces[4];

document.write(newFileName);
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!

But do you also need to handle situations in which the input is not as expected? For example, what happens if the filename contains only a single underscore?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can your original file names have more than four underscores? if so, this won't work. just something to think about.
 
Chris Sanders
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my situation the file name convention will always be the same. I will next work on the other scenerio as it may come up sometime.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic