How can I save the numbers of a notebook in an array in java ?, and then order those numbers.
To recover the data obtained and save them in a array , we can work with the split(String regex)
method, which defines a separator and receives as a parameter a regular expression and returns us String[]
String[] numsStr = s1.split("\n");
And to do the sorting we have the overloaded method java.util.Arrays.sort(numeros)
, which makes us sort in an ascending way
Also in your method leerTxt()
changes the line break you have like this: temp = temp + "\n" + bfRead;
, by this way temp += bfRead+"\n";
, so that you add the string before and then add the line break, not before as you were doing, otherwise you will have problems when converting from Strings to ints
String[] numsStr = s1.split("\n");
int[] numeros = new int[numsStr.length];
for (int i = 0; i < numsStr.length; i++) {
numeros[i] = Integer.parseInt(numsStr[i]);
}
Example:
public class PrincipalCore {
public static void main(String[] args) {
//leer un TXT
Core c = new Core();
String s1 = c.leerTxt("C:\Users\miusuario\Desktop\numeros.txt");
System.out.println(s1 + "");
String[] numsStr = s1.split("\n");
int[] numeros = new int[numsStr.length];
for (int i = 0; i < numsStr.length; i++) {
//if(!numsStr[i].trim().equals(""))
numeros[i] = Integer.parseInt(numsStr[i]);
}
//imprimimos numeros sin ordenar y tal cual los tenemos en nuestro array
for (int numero : numeros) {
System.out.print(numero + " ");
}
System.out.println("");
//ordenamos
java.util.Arrays.sort(numeros);
for (int numero : numeros) {
System.out.print(numero + " ");
}
}
}
class Core {
public String leerTxt(String direccion) {
String texto = "";
try (BufferedReader bf = new BufferedReader(new FileReader(direccion))) {
//StringBuilder temp = new StringBuilder();
String temp = "";
String bfRead;
while((bfRead = bf.readLine()) != null){
//haz el ciclo, mientras bfRead tiene datos
//temp.append(bfRead).append("\n");
temp += bfRead+"\n"; //guardado el texto del archivo
}
texto = temp.toString();
} catch (FileNotFoundException ex) {
System.out.println("No se encontró el archivo");
} catch (IOException ex) {
System.out.println("Excepcion " + ex.getMessage());
}
return texto;
}
}
Result:
Update ..
The readLine()
method in while
, what it will do is return a String of the content read from a line or null 'null' if the end of the sequence has been reached.
Having understood this, and knowing that in the text file the information we have are numbers, we could collect the content of each line, that is, a number and add it to a List 'List' of type < strong> Integer , as they have commented.
public List leerTxt2(String direccion) {
List<Integer> listNums = new ArrayList<>();
try (BufferedReader bf = new BufferedReader(new FileReader(direccion))) {
String bfRead;
while((bfRead = bf.readLine()) != null){
listNums.add(Integer.parseInt(bfRead));
}
} catch (FileNotFoundException ex) {
System.out.println("No se encontró el archivo");
} catch (IOException ex) {
System.out.println("Excepcion " + ex.getMessage());
}
return listNums;
}
And now in the main ..
//Obtenemos nuestra lista
List<Integer> lNums = c.leerTxt2("C:/Users/miusuario/Desktop/numeros.txt");
System.out.println("\nImprimimos tal cual están en la lista");
for (Integer num : lNums) {
System.out.print(num+ " ");
}
//Ordenamos
Collections.sort(lNums);
System.out.println("\nAhora los tenemos ordenados");
for (Integer num : lNums) {
System.out.print(num+ " ");
}
Result: