DisasterCoder
DisasterCoder

Reputation: 577

Java automatic filling of ArrayList, searching for better options

I'm using this code here to automatically fill a string array list with the directory path of obj files for later use in animations, but there's a small problem with this:

private List<String> bunny_WalkCycle = new ArrayList<String>(); 
private int bunny_getWalkFrame = 0;

private void prepare_Bunny_WalkCycle() {

    String bunny_walkFrame = "/bunnyAnimation/bunnyFrame0.obj";

    while(bunny_WalkCycle.size() != 30) { // 30 obj files to loop through

        if(bunny_getWalkFrame != 0) {
            bunny_walkFrame = 
                "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_"+bunny_getWalkFrame+".obj";          
        }

        bunny_WalkCycle.add(bunny_getWalkFrame);

        bunny_getWalkFrame++;
    }
}

Now the problem is that the naming convention in blender for animations has zeros before the actual numbers, so something like this:

bunnyWalkFrame_000001.obj
bunnyWalkFrame_000002.obj
bunnyWalkFrame_000003.obj
...
bunnyWalkFrame_000030.obj

With my prepare_Bunny_WalkCycle method I cannot account for the zeros so I would change the names and get rid of the zeros.. This may be okay for not so many frames but once I hit 100 frames it would get painfull.. So there's my question:

What would be an intelligent way to account for the zeros in the code instead of having to rename every file manually and remove them?

Upvotes: 1

Views: 107

Answers (5)

mohitm
mohitm

Reputation: 163

There are two ways you could do that: Appending the right number of leading zeroes, or using a String formatter.

  1. bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + String.format("%05d", bunny_getWalkFrame) + ".obj";

OR

  1. bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + getLeadingZeroes(bunny_getWalkFrame) + String.valueOf(bunny_getWalkFrame) + ".obj";

where

private String getLeadingZeroes(int walk) {
    String zeroes = "";
    int countDigits = 0;
    while (walk > 0) {
        countDigits++;
        walk /= 10;
    }
    for (int i = 1; i <= (nZeroes - countDigits); i++) {
        zeroes += "0";
    }
    return zeroes;
}

Upvotes: 1

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11642

Here are two options. First, you can use a string formatter to create your filenames with leading zeros:

bunny_WalkCycle.add("/bunnyAnimation/bunnyFrame0.obj");
for (int frame = 1; frame <= 30; frame++) {
    bunny_WalkCycle.add(
            String.format("/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_%06s.obj", frame));
}

The second option is, if you already have all the required files in the directory, you can get them all in one go:

bunny_WalkCycle.add("/bunnyAnimation/bunnyFrame0.obj");
bunny_WalkCycle.addAll(Arrays.asList(new File("/bunnyAnimation/bunnyWalkAnim").list()));

Upvotes: 1

Graphican0
Graphican0

Reputation: 167

I think you can solve your problem with "String.format":

String blenderNumber = String.format("%06d", bunny_getWalkFrame);

Explanation:

  • 0 -> to put leading zeros
  • 6 -> "width" of them / amount of them

And so this would be your new bunny_walkFrame:

 bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + blenderNumber + ".obj";

Upvotes: 3

Nir Levy
Nir Levy

Reputation: 12953

You can use String.format() to pad your numbers with zeros:

String.format("%05d", yournumber);

Upvotes: 3

Araymer
Araymer

Reputation: 1525

Here ya go:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

Just specify how many digits you want. Set it to one. If it has to it will push over (so it won't cut digits off)

Upvotes: 0

Related Questions