0

I'm trying to read a file which consists of some lines and each line has 3 parts: id , name and surname. There is also an EditText where the user needs to enter his id and in case it matches with one of the ones read from the file it should show a dialog like this one: Are you "Name", "Username"?

I've tried doing the following thing but unfortunately it doesn't work.

public String getDNI() {
    String[] parts = fichero.split("\\,");
    String DNI = parts[0];
    return DNI;
}

public String getNombre() {
    String[] parts = fichero.split("\\,");
    String Nombre = parts[1];
    return Nombre;
}

public String getEnunciado() {
    String[] parts = fichero.split("\\,");
    String Apellido = parts[2];
    return Apellido;
}



public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.identificacion);
      Button bSiguiente = (Button) findViewById(R.id.btn_siguiente);
     dniText = (EditText) findViewById(R.id.dni_candidato);

     try {
            InputStream is = getAssets().open(File);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            // Skips lines
            for (i = 0; i<= 100; i++) {
                reader.readLine();

            }
            fichero = reader.readLine();


        } catch (IOException e) {
            e.printStackTrace();
        }



bSiguiente.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {



   try{
       longitud = dniText.getText().toString();
          user = Integer.parseInt(dniText.getText().toString());
          DNIWord(user);
          for (i = 0; i<= 100; i++) {
         if (longitud.equals(getDNI())){
             // (longitud.length()==8){
                     showDialog(DIALOG_CONFIRMAR_CANDIDATO);}

                else{
                 showDialog(DIALOG_ERROR_DNI);
                }

   }
   }
      catch (NumberFormatException e)
    {}
    }
Katherine99
  • 980
  • 4
  • 21
  • 40
  • 1
    I don't see anywhere where your `getDNI()`, `getNombre()`, or `getgetEnunciado()` functions are called. Is there more code we're missing? – Blumer Feb 25 '13 at 20:43

2 Answers2

2

A better approach is to create a class to represent your user:

class User{
    public String nombre;
    public String enunciado;
    public String dni;

   public User(String nombre, String enunciado, String dni){
      this.nombre = nombre;
      this.enunciado = enunciado;
      this.dni = dni;
   }

   public User(String csvLine){
       String[] values = csvLine.split(",");
       this(values[0], values[1], values[2]);
   }

}

I prefer the first constructor because it's easier to read.

Use like this:

ArrayList<User> users = new ArrayList<User>();

...

     String s;
     while ((s = reader.readLine()) != null) {
         users.add(new User(s));
     }

or

 String s;
 String[] value;
 while ((s = reader.readLine()) != null) {
     values = s.split(",");
     users.add(new User(values[0], values[1], values[2])); <-- prefer this one
 }

To make it even better:

public static final int DNI = 0;
public static final int NOMBRE = 1;
public static final int ENUNCIADO = 2;

...

     String s;
     String[] value;
     while ((s = reader.readLine()) != null) {
         values = s.split(",");
         users.add(new User(values[DNI], values[NOMBRE], values[ENUNCIADO]));
     }

Now you can work with your users collection using users.contains, users.getElementAt, Collections.sort, Collections.binarySearch etc.

Simon
  • 14,407
  • 8
  • 46
  • 61
0

Here is another question helpful for parsing CVS in C++: How can I read and parse CSV files in C++?

If you are open to other languages, like python, it might be much easier. For example, python has build-in csv tools: http://docs.python.org/2/library/csv.html

Community
  • 1
  • 1