Infinite loop in java

Asked

Viewed 212 times

0

This my code aims to rename a mass of files to set the following pattern: All compound names, words must start in uppercase. First I give a substring for names that have only one word and then I make one while for compound names but does not leave the while, can anyone help me understand why ? I am trying to rename many files.

import java.io.File;

public class App {

    public static void main(String[] args) {
        File diretorio = new File("/home/douglas/roms");
        File[] arquivos = diretorio.listFiles();

        for (File arquivo : arquivos) {
            String nome;
            String extencao = ".gba";
            nome = arquivo.getName();
            nome = nome.toLowerCase();
            nome = nome.substring(0, nome.indexOf(extencao));
            nome = cortarTrecho(nome, " # gba");
            String primeira = nome.substring(0, 1).toUpperCase();
            String restante = nome.substring(1);
            nome = primeira + restante;

            while (nome.contains(" ")) {

                String nomeFinal = "";
                String partes[] = nome.split("\\s+");

                for (int i = 0; i < partes.length; i++) {
                    nomeFinal += "*" + partes[i].substring(0, 1).toUpperCase() + partes[i].substring(1).toLowerCase();
                }
                nomeFinal = nomeFinal.substring(1) + extencao;
                System.out.println(nomeFinal);
            }

        }

    }

    public static String cortarTrecho(String nome, String trecho) {

        int index = nome.indexOf(trecho);
        nome = nome.substring(0, index);
        return nome;

    }

}
  • 1

    You need to see what you want to do, the output condition involves analyzing the variable nome, as it never changes inside the loop, when it enters, it no longer leaves. It needs to have another condition or change the nome so that at some point it meets this condition. I don’t even know if I should have this while there, I have the impression that neither need, should a if, or even that’s necessary. I didn’t write an answer because I don’t even know what to do.

1 answer

1


It was simple, just assign the value of nameName to name.

nome = nomeFinal;
  • That doesn’t make much sense, actually other things in the code don’t make much sense, or Pleo less is a waste.

  • @bigown got it, I’m learning little by little yet rsrs, but thanks.

  • You can change your question a little bit so @bigown can give you a hint of what doesn’t make sense and how to fix it

  • @mustache ready.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.