I am trying to make a little search method, and up to now I have one that only searches int types, I've been wondering if its somehow possible to re implement this method to search Strings?
//Searches only ints
public void buscarCliente_Codigo(int cod) throws IOException{ /*cod is for the input
for eg. a textfield input which im using*/
try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw"); //.txt File name
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f); //Reads String
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(cod==codigo){
iCodigoBusqueda = codigo;
encontrado=true;
registroExistente = true;
break;
}else{
iCodigoBusqueda = 0; //Type int
registroExistente = false;
}
bytes +=1;//Changes
f.seek(bytes);
}
while(bytes<f.length());
f.close();
} catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");
}
}
After doing that I just code a couple of .getText and I have a beautiful search mechanism that only works with number, but I really want a method that searches for Strings, So I made a couple of tweeks:
**enter code here**
public void buscarCliente_Nombre(String nom) throws IOException{ //change to String
try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw");
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f);
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(nom.equals(nombre)){
iNombreString = nombre;
encontrado=true;
registroExistente = true;
break;
}else{
iNombreString = ""; //String type
registroExistente = false;
}
bytes +=1;//cambios
f.seek(bytes);
}
while(bytes<f.length());
f.close();
} catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");
}
}
It doesn't work, it gives me the Exception.
So is there anyway this code would work for Strings_?.
thanks
You can find substrings in a string? yes.
Honestly i have no idea what your trying to do there, and either I know what a "RandomAccessFile" is but if it's just a regular file, why not just read it like one.
//Read the file... I don't bother to show how...
string = //the data of the file...
if(string.contains("Substring")) {
System.out.println("Substring could be found");
}
else {
System.out.println("Substring could not be found");
}
If you're trying to read something like xml, I'd recommend
if(string.contains("Substring=\"")) {
int pos = string.indexOf("Substring=\"")+"Substring=\"".length();
return string.substring(pos,string.indexOf("\"",pos));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.