This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Distributing files in round robin manner  RSS feed

Sukhada Pawar
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have written a program that moves file from "inputdir" to say 3 different directortries (destination directory are configurable i.e it can be more then 3 or less) d1, d2, d2 in round robin manner.

for example: suppose inputdir contains 4 files i.e f1,f2,f3,f4 .

Files must be distributed in following manner

d1 -f1, d2-f2, d3-f3, d1-f4

And again, if we add 4 more files in inputdir say f5,f6,f7,f8 then distribution should be


But my program, starts the next distribution again from d1, which is wrong way, it should start the distribution from d2

Here is the code.

Can anyone help me in this.
Greg Charles
Posts: 3015
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Java Ranch!

First and foremost, always UseCodeTags when posting code. I've added them here to make your code easier to read.

Now, you're just trying to do too much in one single function and that makes everything confusing. You had the right idea when you created the moveFromInputDirectoryToProcessingDirectory() function, but you need more functions like that. That way you can break your problem down into manageable chunks. It's also a good strategy for maintainability. What if your next assignment asks you to change the way the round robin distribution works? Wouldn't it be easier if you had a single small function to modify, rather than digging through the entirety of building a list of directories, looping through files, copying files to destination folders, etc.? (Hint: yes, it would be easier.)

OK, so I'm telling you that you need a function called, for example, int computeDestinationIndex(int, int). The parameters to this method should be the file index and the total number of destination directories. Assuming 3 destination directories, you want file indexes 0,1,2,3,4,5,6,7,8,9,... to map to directory indexes 0,1,2,1,2,0,2,0,1,0,... You're right that the % operator is going to be part of the formula, but it's not the whole thing. As you've seen, that gives you 0,1,2,0,1,2,0,1,2,0,... A little trial and error will help you get it right. Having a separate function will also make it easier to test.

Finally, I'm not a fan of the if-else on lines 15-22. It's true that you don't want to do something % 0. However, if you have 0 destination directories, you have bigger problems. You should probably handle it by erroring out of the whole process with some kind of error message.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!