Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

renameTo alternative?

  • 19-11-2012 8:29am
    #1
    Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭


    Hey, I got a problem,I made a program using java which takes russian named files and renames them into english. But the problem is: When it comes to a loop renaming say 50 (for example) files, it only renames 25 of them. Then if I try again it renames 12 of them. Then 6. then 3 etc..

    Any alternatives?
    public void Rename(String name, File which){
            which.renameTo(new File(desti+"\\"+name)); 
        }
    
    This function is called by loop which should rename all files.


Comments

  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    The most important part for debugging is missing: the loop.


  • Closed Accounts Posts: 4,372 ✭✭✭im invisible


    who spiked the hamsters water?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    I think it's some sort of loop joke :pac:


  • Closed Accounts Posts: 4,372 ✭✭✭im invisible


    it wasn't the only thread in the last 10 minutes to have the same post multiple times
    .
    .
    it wasn't the only thread in the last 10 minutes to have the same post multiple times

    etc.


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    So any solutions?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    <multiple post mode off> ;)

    Show us the loop


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    PrzemoF wrote: »
    <multiple post mode off> ;)

    Show us the loop
            for(int a = 0; a < ch.length(); a++){
               String newName = "";
                for(int i = 0; i < ch.getNumber()[a].length(); i++){
                    char letter = ch.getNumber()[a].charAt(i);
                    String let = Character.toString(letter);
                    newName = newName+= ru.toEng(let);
                }
                ou.Rename(newName, ch.getFile(a));
            }
    


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    How do you set "ch"?
    "ch.length()" equals the number of files for renaming?
    What happens if you replace ch.length() with a fixed number - does it work OK then?


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    PrzemoF wrote: »
    How do you set "ch"?
    "ch.length()" equals the number of files for renaming?
    What happens if you replace ch.length() with a fixed number - does it work OK then?

    ch.length is correct, I tried outputting it to console and it displayed number of file okay


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    arleitiss wrote: »

    ch.length is correct, I tried outputting it to console and it displayed number of file okay

    If ch.length is correct then where is it stopping.
    Do a console output of the value of ch.length and the value of a after each loop and see where its breaking.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    If ch.length is correct then where is it stopping.
    Do a console output of the value of ch.length and the value of a after each loop and see where its breaking.
    As I said: if I leave the renameTo line in, it stops half way. So if ch.length is 50, it will stop at 25. If I take out renameTo function ti goes all the way through no problem.


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    arleitiss wrote: »
    As I said: if I leave the renameTo line in, it stops half way. So if ch.length is 50, it will stop at 25. If I take out renameTo function ti goes all the way through no problem.

    Loops don't just stop so there must be an exception happening somewhere in your rename.

    Are you trapping any exceptions?


  • Registered Users, Registered Users 2 Posts: 203 ✭✭Sherfin


    Not done Java in a while so not 100% sure if this is the problem but..
    Does it just rename every 2nd file?
    If so, the list may be changing for each loop.

    e.g. List is FileA, FileB, FileC, FileD

    1st pass int a=0 So it changes file[0] FileA to NewFileA
    List has now changed to FileB, FileC, FileD as FileA no longer exists
    2nd pass a=1 so it changes file[1] FileC to NewFileC (FileB is at position [0] now)
    List now comprises of FileB, FileD
    On the 3rd pass int a =2 which is the length of the list and the loop ends

    Hope this makes sense. To prove/disprove this, you could output the length on each pass to see if it is decreasing.


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    Yeah it skips every 2nd file. So skips 1. ALso I don't throw any exceptions.


  • Registered Users, Registered Users 2 Posts: 203 ✭✭Sherfin


    Then it could very well be what I tried to explain above.
    If so, a list with an odd number of items will throw an exception (OutOfBounds I think ?) when it reaches halfway (or halfway +1)


    There is a way around this problem
    Try looping backwards. i.e. for(int a =ch.length(); a >0; a--)


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    Sherfin wrote: »
    Then it could very well be what I tried to explain above.
    If so, a list with an odd number of items will throw an exception (OutOfBounds I think ?) when it reaches halfway (or halfway +1)


    There is a way around this problem
    Try looping backwards. i.e. for(int a =ch.length(); a >0; a--)
    Didn't work, it said:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException


  • Registered Users, Registered Users 2 Posts: 203 ✭✭Sherfin


    Sorry, as I said it's been a while since I did any java.
    If this happens at the first pass in the loop then try for(int a =ch.length()-1; a >0; a--)


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    Sherfin wrote: »
    Sorry, as I said it's been a while since I did any java.
    If this happens at the first pass in the loop then try for(int a =ch.length()-1; a >0; a--)

    Ohh nice one sir, that worked except for 1 problem, but I can fix it myself. It misses 1 file but it's not big of a deal, it's just a matter of writing a > -1 (I think) but yeah it works, oh my god never thought of loops being used in reverse. Thank you very much.
    Yep a > -1 did that.
    I don't see the logic or difference behind this, but as people say sometimes: if it works, don't touch it.


  • Registered Users, Registered Users 2 Posts: 203 ✭✭Sherfin


    Glad to help.
    It probably should be a>=0

    The problem is that the list of files is changing every time you loop.
    There were 50 items (filenames) in your original list
    Each time you changed an item, it was no longer in the list.
    As 'int a' counted up, the list got shorter by one.
    (when a=0 length =50, a=1 length =49 ........ a=25 length =25)
    This is why it kept stopping halfway.
    By counting backwards it didn't matter that the list was getting shorter
    (when a=50 length =50, a=49 length =49 ........ a=1 length =1)


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    Sherfin wrote: »
    Glad to help.
    It probably should be a>=0

    The problem is that the list of files is changing every time you loop.
    There were 50 items (filenames) in your original list
    Each time you changed an item, it was no longer in the list.
    As 'int a' counted up, the list got shorter by one.
    (when a=0 length =50, a=1 length =49 ........ a=25 length =25)
    This is why it kept stopping halfway.
    By counting backwards it didn't matter that the list was getting shorter
    (when a=50 length =50, a=49 length =49 ........ a=1 length =1)
    ohh I see, well thanks, thats solved.


  • Advertisement
Advertisement