Read a TXT file, sort, and save a new Java file

Asked

Viewed 4,021 times

0

I’m doing a job for college, and I’m not being able to read the file and throw the dice into a Arraylist to sort the data, below follows as is my code so far

public class Teste {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> vet = new ArrayList<>();
    String aux = null;
    int i = 0;
    vet.add("nome");
    vet.add("cidade");
    vet.add("estado");
    vet.add("aula");
    vet.add("cifra");


      BufferedReader lerArq = new BufferedReader(new FileReader("Teste.txt"));
      String s;
      int n = 0;
      try{
          while ((s = lerArq.readLine()) != null){
              n++;
          }
          System.out.println(s);
      }
   catch(Exception e){
       System.out.println("Excecao1\n");
   }

    System.out.println("Vetor desordenado: ");
    for (i = 0; i < 5; i++) {
        System.out.println(" " + vet.get(i));
    }
    System.out.println(" ");
    for (i = 0; i < vet.size(); i++) {
        for (int j = 0; j < vet.size()-1; j++) {
            if (vet.get(j).compareToIgnoreCase(vet.get(j + 1)) > 0) {
                aux = vet.get(j);
                vet.set(j, vet.get(j + 1));
                vet.set(j + 1, aux);
            }
        }
    }
    System.out.println("Vetor organizado:");
    for (i = 0; i < vet.size(); i++) {
        System.out.println(" " + vet.get(i));

    }
    int x = 1;
     System.out.println(vet.get(x-1));
}
}

The ordination is working, I’m just not getting to use the Filereader, some tip on how to play the dice inside the Arraylist?

Remember, the data contained in txt is the same as below, 5 words, each in a new line.

    vet.add("nome");
    vet.add("cidade");
    vet.add("estado");
    vet.add("aula");
    vet.add("cifra");
  • how are words in the? file separated by lines or on the same line? or is that not relevant?

  • Note this, it is important (maybe not for your problem but forever): http://answall.com/a/30168/101. Is the reading showing an error? Or is printing the data read correctly and you just can’t get the data read on ArrayList? I imagine these 5 adds were placed only to test, right?

  • They’re separated by line. bigow, the reading is working, I’m just not able to insert the data read inside my arraylist, for this to do the ordering.

  • Has a way of ordering without arraylist?

1 answer

1


If the words were one on each line, you can use this code:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
        String line = br.readLine();

        while (line != null) {
            vet.add(line);
            line = br.readLine();
        }
    }
  • your code worked, I was able to read the file, show on the canvas cluttered, sort, and show on the ordered screen. Thank you!

  • if your problem has been solved can mark the answer as sure...

  • 1

    I forgot... I was making other adjustments. I marked now!

Browser other questions tagged

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