I have made the following code (there may be a more correct way to do it, but this is not my problem):
package activitat2;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
/**
* ACTIVITAT 2 EAC 6
* @author Montse
*/
public class EquipIns{
public static void main(String[] args) {
// TODO code application logic here
EquipIns programa = new EquipIns();
programa.inici();
}
public void inici(){
System.out.println("Institutos....");
System.out.println("1-Registrar los datos de los institutos ");
System.out.println("2-Listar todos los institutos");
System.out.println("3- 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("Registra los datos de los institutos:");
System.out.println("Quantos institutos participan en el concurso?");
boolean leido=false;
int enterLlegit2 =0;
while(!leido){
leido = lector.hasNextInt();
if (leido){
enterLlegit2 = lector.nextInt();
System.out.println("Numero total de centros: "+enterLlegit2);
for (int j = 0; j < enterLlegit2; j++) {
int i=0;
i = enterLlegit2;
if(enterLlegit2>0){
System.out.println("-------------------------------------");
System.out.println("Introduce la info sobre el instituto:");
System.out.println("-------------------------------------");
System.out.println("Id?");
int idInst = lector.nextInt();
System.out.println("Nom Institut:");
String nomInst = lector.next();
System.out.println("Nom Equip:");
String nomEqui = lector.next();
System.out.println();
System.out.println("------------------------------------------");
System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
System.out.println("------------------------------------------");
System.out.println("Id: " +idInst);
System.out.println("Nom Institut: "+nomInst);
System.out.println("Equip: "+nomEqui);
System.out.println("Punts: "+punts);
System.out.println();
}
}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
break;
case 2:
System.out.println("has escogido listar los institutos");
break;
case 3:
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();
}
}
I need to create a binary file from this file to store the data entered by the user.
It puts me in the exercise that I have to enter the records with a fixed measure using the void Seek()
. Maximum 48 bytes (id (4), Institute (20), TeamName (20), Points (4) = 48).
In other words, once the data has been entered by the user, they must be recorded in the file.
Later I have to show this information.
EDIT 1
I have one more question!
I have done the part of the code that the colleague has given me and it gives me only one result per screen because I suppose that I use the same variable for each response from the user.
I put code:
package activitat2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* ACTIVITAT 2 EAC 6
* @author Montse
*/
public class EquipIns{
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
EquipIns programa = new EquipIns();
programa.inici();
}
public void inici() throws FileNotFoundException, IOException{
RandomAccessFile raf;
raf = new RandomAccessFile("Resultat.bin", "rw");
System.out.println("Institutos....");
System.out.println("1-Registrar los datos de los institutos ");
System.out.println("2-Listar todos los institutos");
System.out.println("3- 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("Registra los datos de los institutos:");
System.out.println("Quantos institutos participan en el concurso?");
boolean leido=false;
int enterLlegit2 =0;
while(!leido){
leido = lector.hasNextInt();
if (leido){
enterLlegit2 = lector.nextInt();
System.out.println("Numero total de centros: "+enterLlegit2);
for (int j = 0; j < enterLlegit2; j++) {
int i=0;
i = enterLlegit2;
if(enterLlegit2>0){
System.out.println("-------------------------------------");
System.out.println("Introduce la info sobre el instituto:");
System.out.println("-------------------------------------");
System.out.println("Id?");
int idInst = lector.nextInt();
System.out.println("Nom Institut:");
String nomInst = lector.next();
System.out.println("Nom Equip:");
String nomEqui = lector.next();
System.out.println();
System.out.println("------------------------------------------");
System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
System.out.println("------------------------------------------");
System.out.println("Id: " +idInst);
System.out.println("Nom Institut: "+nomInst);
System.out.println("Equip: "+nomEqui);
System.out.println("Punts: "+punts);
System.out.println();
raf.seek(0);
raf.writeInt(idInst);
raf.seek(4);
raf.writeUTF(nomInst);
raf.seek(24);
raf.writeUTF(nomEqui);
raf.seek(44);
raf.writeInt(punts);
}
}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
break;
case 2:
System.out.println("has escogido listar los institutos");
raf.seek(0);
System.out.println(raf.readInt());
raf.seek(4);
System.out.println(raf.readUTF());
raf.seek(24);
System.out.println(raf.readUTF());
raf.seek(44);
System.out.println(raf.readInt());
raf.close();
break;
case 3:
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();
}
}
How do I solve it?
I try to explain better I think the error is:
When the user inserts Teams and institutes, he saves it in the same variable. So as soon as I save that data in the variable again, it deletes the previous one.
How do I correct it?
Can you give me an example?
It is the Switch Case (option 1) First I ask for the number of Institutes that participate, Then ID, Then Institute Name Then Team name.
In the Switch Case 2 the document is ready! Is there any way that I don't get an error when it's empty? Putting some kind of text saying that there are no institutes entered?
THANK YOU!!
EDIT 2
raf.seek(0);
raf.writeInt(id);
raf.seek(4);
raf.writeUTF(instituto);
raf.seek(24);
raf.writeUTF(equipo);
raf.seek(44);
raf.writeInt(puntos);
// Leer datos
raf.seek(0);
System.out.println(raf.readInt());
raf.seek(4);
System.out.println(raf.readUTF());
raf.seek(24);
System.out.println(raf.readUTF());
raf.seek(44);
System.out.println(raf.readInt());
One more question...
When you put Read UTF on me, I imagine that the UTF is because it is String data?
In order to use the method
seek
, you need an instance, for this particular case, ofjava.io.RandomAccessFile
. This class is used to access a file randomly, either for reading or writing.The method
seek
puts the pointer of the file—from which it will be read or written—at the position indicated bypos
. This position indicates the number of bytes to scroll through the file, where 0 is the beginning of the file.The following code exemplifies the use of the class
java.io.RandomAccessFile
and its methodseek
:Code:
Output :
UPDATE
If you plan to write that same 48-byte block multiple times, you need to offset the corresponding number of bytes. For example, you could add the variable
pos
to save the next writing position. Namely.You can do something similar in reading:
writeUTF
andreadUTF
are used for writing or reading character strings. In Java, achar
uses two bytes. So in 20 bytes, you can only put maximum 10char
s. UTF is more closely related to Unicode and transformation formats. Basically it refers to the way of representing a character in bits, which is very different between different character encoding standards (UTF-8, ISO-8859-1).