1

im using this to display a image in a certain folder and subfolder, everything "works fine", the files are named in this form ex:

17000.001 17000.002 18555.001 18542.001 1.001 1.002 1.003 1.004 .....

the .xxx is the extension (a renamed TIFF)

the program works in this way:

You Type the Number you want, example: i want the 17000, i type 17000, and it return the FIRST .001 in the screen, and the others .002, .003 and how many it have, i want to walk throught it by a next image button and previsoly image...

the problem is: when i try to search for a number that have more than 4 .004 or more, it dont display the first, it display "random", .002, 004 or other i cant understand why, this is the piece of the code where i get the "path" to it!! dont kill me because the code ^^!

....

        public void geraListaArquivos(String subdir, String matricula) {


            String diretorio = "F:\\registro_sql\\Imagens\\Livro02" + "\\"

     + subdir + "\\";
            String novaimagem = null;

            File folder = new File(diretorio);
            listOfFiles = folder.listFiles();
            if (!folder.exists()) { 
                JOptionPane.showMessageDialog(null,"Não existe o diretório em que está tentando fazer a busca");
            } else {
        //  JOptionPane.showMessageDialog(null, diretorio);
            for (int i = 0; i < listOfFiles.length; i++) {  

                String matsonome[] = listOfFiles[i].getName().split("\\.");

                for (int i2 = 0; i2 < matsonome.length; i2 = i2 +2) {

                    if(matsonome[i2].matches(matricula)) {
                        System.out.println(matsonome[i2] = "." + matsonome[i2+1]);
... the rest of the code, if the typed number image exist in the folder

i dit the String matsonome to check if the first part of the array matches the typed number,.. i2 +2, coz wwhen it split for example 17000.001 and 17000.002

will be in this way:

matsonome[0] = 17000
matsonome[1] = 001
matsonome[2] = 17000
matsonome[3] = 002

in this case the "System.out.println(matsonome[i2] = "." + matsonome[i2+1]);"

will display correct cuz it have less than 4

17000.001 17000.002

but if the typed number have 4 or more, it display in this way(out of order):

xxxx.002 xxxx.001 xxxx.004 xxxx.003

why???

sorry the bad english :(

user2582318
  • 1,607
  • 5
  • 29
  • 47
  • 1
    Please learn to read the documentation when you have questions like this. Here's a quote from the documentation for [`File.listFiles()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles()): "_There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order..._" You should just sort the array of strings yourself. – jahroy Aug 23 '13 at 18:41

1 Answers1

2

I believe it's because the order isn't guaranteed (if I correctly understand the question).

See the documentation:

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

That means you'll have to sort the array using a static function.

Community
  • 1
  • 1
sdasdadas
  • 23,917
  • 20
  • 63
  • 148