ok so i have a project for school, and i finished so the teacher wants it to read for a .dat the parameters for the java class here is what i got:

public void cargarCurso(){
        try{
            
            BufferedReader fileIn = new BufferedReader(
                    new FileReader("c:/cursoSem2.dat"));
            String dataRenglon = fileIn.readLine();
            while(dataRenglon !=null){
                StringTokenizer stknzr = new StringTokenizer(dataRenglon,"_");
                String clave = stknzr.nextToken();
                String nombre = stknzr.nextToken();
                String tipo = stknzr.nextToken();
                String dni = stknzr.nextToken();
                String despacho = stknzr.nextToken();
                String departamento = stknzr.nextToken();
                String dia = stknzr.nextToken();
                String hora = stknzr.nextToken();
                if(clave.charAt(0)=='P'){
                    Profesor profesor = new Profesor(
                            nombre,dni,despacho,departamento);
                    curso.addProfessor(profesor);
                }else if(clave.charAt(0)=='A'){
                    Asignatura asignatura = new Asignatura(
                            nombre,tipo);
                    curso.addAsignatura(asignatura);
                }else if(clave.charAt(0)=='H'){
                    Horarios horario = new Horarios(
                            dia,hora);
                    curso.addHorario(horario);
                }dataRenglon = fileIn.readLine();
            }

        }catch(FileNotFoundException fnf){
            System.err.println("Archivo no encontrado");
        }catch(IOException ioe){
            System.out.println("Error de lectura");
        }
    }

but is says at the end No such element exception

this is the information in the dat:

P_Erica Magano del Rio_EMR123_Contabilidad_C1
P_Gustavo_GCC123_Informatica_C2
P_Rosa_RMP123_Informatica_O1
P_Olmos Trejo_OMP123_Informatica_P1
P_Salvador Valerio_SVV123_Informatica_D1
P_Alex vargaz_PMP123_Informatica_P2
A_Contabilidad_Teorico
A_Calculo_Practico
A_OAC_Teorico
A_Programacion I_Practico
A_Diseno Interfaces_Practico
A_Diseno Web_Ambas
H_Lunes,Miercoles_7-9
H_Lunes,Miercoles_9-11
H_Martes,Jueves_7-9
H_Lunes,Miercoles_11-1
H_Martes,Jueves_9-11
H_Martes,Jueves_11-1

sorry it's in spanish but clave = code that will tell it if it's a P - Professor
A-Class and H-Schedule and each class has diferent parameters so i dont know if thats allowed for the string tokenizer and external reading, thanks for any help
(:

is says at the end No such element exception

Please copy and paste here the FULL text of the error message. It has more information about the error than you show.

Check your data to be sure there are enough tokens for each nextToken() to get one.

Exception in thread "main" java.util.NoSuchElementException
        at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
        at olmosProyecto.GestionDeHorarios.cargarCurso(GestionDeHorarios.java:44)
        at olmosProyecto.GestionDeHorarios.<init>(GestionDeHorarios.java:23)
        at olmosProyecto.GestionDeHorarios.main(GestionDeHorarios.java:83)
Java Result: 1

this is the structure of each class
Professor:

public Profesor(String newNombre, String newDni, String
            newDespacho, String newDepartamento){
        nombre = newNombre;
        dni = newDni;
        despacho = newDespacho;
        departamento = newDepartamento;
    }

Class

public Asignatura(String newNombre, String newTipoAsig){
        nombre = newNombre;
        tipoAsig = newTipoAsig;
    }

so i declared 8 tokens, but not all classes are composed of 8 tokens, can the specified token in the parameters of each class be what they get assigned??

at olmosProyecto.GestionDeHorarios.cargarCurso(GestionDeHorarios.java:44)

That line from the error message tells you which line of your source code (line 44 in GestionDeHorarios.java) threw the error, so you can see what token it was looking for on that line, which should help you identify the error.

ps
Not all lines in the data have 8 tokens, so you can't try to read 8 from every line. Read the first token to see what kind of record it is, then read the correct number of tokens for that record type and create the appropriate object.

Edited 5 Years Ago by JamesCherrill: ps

Read the API documentation for the StringTokenizer class. It has methods to test the number of tokens and if there are more available.

Just a thought...
The best-encapsulated way to implement this may be to have a constructor in each class that takes a StringTokeniser as a parameter - that way all the knowledge about how many fields there are and how to parse and save them is where it should be - in the class itself...

StringTokenizer stknzr = new StringTokenizer(dataRenglon,"_");
String clave = stknzr.nextToken();
switch (clave.charAt(0)) {
   case 'P': Profesor profesor = new Profesor(stknzr);  break;
   //etc
}
...
    public Profesor(StringTokenizer stknzr){
       nombre = stknzr.nextToken();
       dni = stknzr.nextToken();
       despacho = stknzr.nextToken();
       departamento = stknzr.nextToken();
       ...
    }
This question has already been answered. Start a new discussion instead.