Stephan
Stephan

Reputation: 19

DataInputStream isn't reading properly?

I'm fairly new to both Java programming & stack overflow, and this morning ive been having a big trouble with following simple code. It has 3 options, add new user to a binary file, list every user on it and searching a specific user to show every other data related to it (last name & year born):

case 1: System.out.println("Introduce nombre: ");
        nombre=teclado.next();
        System.out.println("Introduce apellido: ");
        apellido=teclado.next();
        System.out.println("Introduce año de nacimiento: ");
        nacido=teclado.nextInt();
        dos.writeUTF(nombre);
        dos.writeUTF(apellido);
        dos.writeInt(nacido);
        break;
case 2: try {
            FileInputStream fis=new FileInputStream("file.bin");
            DataInputStream dis=new DataInputStream(fis);
            while (dis.available()>0) {
                   System.out.println(dis.readUTF()+" "+dis.readUTF()+" nació en "+dis.readInt());
            }
            fis.close();
       }
       catch (EOFException e) {System.out.println("Fin del fichero.");}
       break;
case 3: System.out.println("Introduce el nombre a buscar: ");
        String buscar=teclado.next();
        try {
            FileInputStream fis=new FileInputStream("file.bin");
            DataInputStream dis=new DataInputStream(fis);
            while (dis.available()>0) {
                   if (buscar.compareTo(dis.readUTF())==0) {
                           System.out.println("Los datos completos del usuario son: "+dis.readUTF()+" "+dis.readUTF()+" que nació el "+dis.readInt());
                           break;
                   }
            }
            fis.close();
        }
        catch (EOFException e) {System.out.println("user not found.");}
        break;

Add user & list them all are working ok, however the searching option isn't. In case 2 loop is reading fine the whole file though on case 3 it is Reading just 1-2 words and already shows the "user not found", any advice on why?

Regards.

Upvotes: 0

Views: 556

Answers (1)

PKey
PKey

Reputation: 3841

So your problem is that your user not found message is printed when there is an unexpected end of file.

This means that you try to read from file when there is nothing else left to read.

The problem lies in this chunk of code:

if (buscar.compareTo(dis.readUTF())==0) {
                           System.out.println("Los datos completos del usuario son: "+dis.readUTF()+" "+dis.readUTF()+" que nació el "+dis.readInt());
                           break;
                   }

Here you read once to compare and then attempt to read again in order to print out (and that is the problem).

What you need to do is to read each field only once - and assign its value to a variable if you want to use it again (e.g. print).

So, something like this, should work:

String first=dis.readUTF();
String second=dis.readUTF();
int    third=dis.readInt();
if (buscar.compareTo(first)==0) {
                           System.out.println("Los datos completos del usuario son: "+first+" "+second+" que nació el "+third);
                           break;
                   }

*By the way, in your implementation - there is high probability that user will not be found - but because no Exception will be thrown - no message will be displayed either - you should fix that too.

Upvotes: 1

Related Questions