aspose file tools*
The moose likes Beginning Java and the fly likes Changing filename Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing filename" Watch "Changing filename" New topic
Author

Changing filename

Chris Sanders
Greenhorn

Joined: Jan 05, 2010
Posts: 3
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)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to the ranch!

I would tell you exactly what fred said above. Follow that advice!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Chris Sanders
Greenhorn

Joined: Jan 05, 2010
Posts: 3
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

Joined: Aug 31, 2004
Posts: 11343

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
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

can your original file names have more than four underscores? if so, this won't work. just something to think about.
Chris Sanders
Greenhorn

Joined: Jan 05, 2010
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing filename