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?
– jsantos1991
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 5add
s were placed only to test, right?– Maniero
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.
– CrazyNomad
Has a way of ordering without arraylist?
– Maurício Z.B