1
私はちょっと検索メソッドを作ろうとしていますが、これまではint型しか検索していませんでしたが、このメソッドを実装してStringを検索することが可能かどうか疑問に思っていましたか? :私はちょうど.getTextのカップルをコーディングし、私は数だけで動作する美しい検索メカニズムを持っていますが、私は本当に文字列を検索する方法をしたい、だから私はtweeksのカップルを作ったことをやった後このコードでRandomAccessFileの文字列を検索する方法は?
//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");
}
}
**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");
}
}
それは私に例外を与えます。
このコードはStrings_?で動作します。
おかげ
をお勧めしますか? –
は、あなたのサイトの購読を中止することがあります – DaveRlz