I have the following code:
try{
raf = new RandomAccessFile("Partides.dat", "rw");
for (long i=0; i< "partides.dat".length(); i= i+48){
for (long j=i +48; j<"partides.dat".length(); j=j+48){
// Es llegueixen els valors de la posició punts
raf.seek(i);
double valorI= raf.readDouble();
raf.seek(j);
double valorJ = raf.readDouble();
//Es comparen
if (valorI > valorJ )
//se intercanvian
raf.seek(i);
raf.writeDouble(valorJ);
raf.seek(j);
raf.writeDouble(valorI);
}
}
raf.close();
I can't get it to sort the records I have... and I don't know what I'm doing wrong... :S In this case I'm trying to sort two values...
Basically, with the code, I try to get it to order some data that is positioned at 48 "that is, the points" from highest to lowest. But if I understand my code correctly I am trying to sort only 2 values... I don't know if there is a way to sort all of them in another way... :S
I put the complete code for review:
/**
* OPCIO 2 ACTIVITAT 3
*/
package activitat2;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
*
* @author Montse
*/
public class Opcio2 {
/**
* En aquesta clase farem les demandes de l'activitat 2
* @throws FileNotFoundException
*/
static void ActualitzarInstituts() throws FileNotFoundException, IOException {
RandomAccessFile raf;
raf = new RandomAccessFile("Partides.dat", "rw");
System.out.println("--------------------------------");
System.out.println("HAS ESCOLLIT OPCIÓ 2");
System.out.println("--------------------------------");
System.out.println("Escoge una opción:::::::");
System.out.println("1-Actualizar Puntos y listar resultados");
System.out.println("2-Instituto Ganador ");
System.out.println("3-Percentatge dels millors instituts");
System.out.println("4- Volver al menú principal");
System.out.println("5- Salir");
System.out.println("Escoje una opción");
Scanner lector = new Scanner(System.in);
int punts=0;
int enterLlegit =0;
boolean llegit = false;
while(!llegit){
llegit = lector.hasNextInt();
if (llegit){
enterLlegit = lector.nextInt();
switch (enterLlegit) {
case 1:
System.out.println("--------------------------------------");
System.out.println("HAS ESCOGIDO ACTUALIZAR PUNTOS");
System.out.println("--------------------------------------");
for (int registro = 1;; registro++) {
int pos = 48 * (registro - 1);
try {
pos += 4 + 20 + 20;
// Lectura
raf.seek(pos);
System.out.printf("Instituto número: #%02d%n", registro);
raf.seek(pos);
System.out.printf("Puntos actuales: %d%n", raf.readInt());
// Escritura
raf.seek(pos);
System.out.println("Introducir puntos: ");
punts = lector.nextInt();
System.out.println("-------------------------------------");
raf.writeInt(punts);
raf.seek(pos);
} catch (EOFException e) {
// Fin de archivo
raf.close();
System.out.println("Datos actualizados:::");
System.out.println("Tornem al menú principal");
Opcio1.IntroduirInstituts();
break;
} }break;
case 2:
System.out.println("-------------------------------------");
System.out.println("HAS ESCOGIDO ORDENAR POR PUNTOS:");
System.out.println("Instituto Ganador ");
System.out.println("-------------------------------------");
try{
raf = new RandomAccessFile("Partides.dat", "rw");
for (long i=0; i< "partides.dat".length(); i= i+48){
for (long j=i +48; j<"partides.dat".length(); j=j+48){
// Es llegueixen els valors de la posició punts
raf.seek(i);
double valorI= raf.readDouble();
raf.seek(j);
double valorJ = raf.readDouble();
//Es comparen
if (valorI > valorJ )
//se intercanvian
raf.seek(i);
raf.writeDouble(valorJ);
raf.seek(j);
raf.writeDouble(valorI);
}
}raf.close();
// ANEM A LLISTAR RESULTATS:
System.out.println("Ves a la opción Listar para observar los cambios");
Opcio1.IntroduirInstituts();
} catch (Exception e){
System.out.println("Error ordenado fichero");
}
break;
case 3:
System.out.println("Percentatge dels millors instituts");
//PENDIENTE DE HACER //
break;
case 4:
System.out.println("TORNAR AL MENÚ PRINCIPAL::::");
MenuPrincipal.EscollirOpcio();
break;
case 5:
System.out.println("HAS ESCOLLIT SORTIR");
System.out.println("Hasta la próxima!");
break;
default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
break;}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
lector.nextLine();
}
You could order the records using the interface
java.lang.Comparable
and the classjava.util.Collections
.First you create a class that implements the mentioned interface. For example:
The implementation of the method
compareTo
is used in the ordering process.To store all records, a list can be used, i.e. an instance of
java.util.List
:In a loop
for
, you create aRegistro
, read the corresponding data and add it to the list, until the file is finished:You sort the list using the
sort
class methodjava.util.Collections
:Once sorted, you write the records to the file with another loop
for
:Since I don't know if you can use
Arrays sort
it or if you can implement itComparator<Comparable[]>
to sort an array, I'll show you another way:As I told you this code has not been compiled, if it works without errors I do not rule it out, but it would be strange, even so you can take it as a pseudo code, on the other hand you can read some of the doubts in the comments within the code, I hope May it help you or at least help you.