954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java external data

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
(:

pxndx
Newbie Poster
20 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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??

pxndx
Newbie Poster
20 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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();
       ...
    }
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thanks guys

:D

pxndx
Newbie Poster
20 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: