how to read character by character in java?

0

Sorry I have a question, I have to do a class that reads a .txt grades and print the name of the student with the highest average, the problem is that only read line by line and do not know how to read character by character can make you read a score of 2-3 digits and save them in a variable just like the name

The document should be like this

Alarcón 75 80 90 50 75 100

Galicia 50 75 50

Valencia 80 95 50 100

then read the grades and take the average and print on the screen the name of the student with the highest rating

The only thing I have is this class that I did but it is to read line by line

    public static String[]   cargarArreglo (InputStream is, int n) throws IOException{
    int i=0;
    int j = 0;
      String [] v = new String [n];
    InputStreamReader isr = new InputStreamReader (is);
    BufferedReader br = new BufferedReader (isr);
    String linea = br.readLine();
    while ((linea != null) && (i <n)) {
        v[i] = linea;
        linea=br.readLine();
        i++;


    }
    br.close();
      for(j =0;j<n;j++){
            System.out.println(v[j]);

          }

    return v;
}
    
asked by lalo hernandez 18.04.2018 в 05:28
source

1 answer

0

You do not have to read character by character to get what you need. Reading line by line, as you do, you're doing fine, just use the split() method of the class String to separate the claficaciones and the name. Just keep in mind that, according to the example of the structure of the document you share, each student can have a variable number of grades, so I recommend creating a class Alumno that manages the name of each student and a list of their notes; and that from this class you develop the mechanisms to obtain the data you need.

public class Alumno {
    private String nombre;
    private List<int> calificaciones;// pongo int porque tu ejemplo muestra números enteros solamente

    public Alumno(String nombre, List<int> calificaciones) {
        // Notar que no valido los parámetros pero deben validarse
        this.nombre = nombre;
        this.calificaciones = calificaciones;
    }

    // Métodos getters y setters
}


public static List<Alumno> cargarAlumnos(InputStream is) throws IOException {       
    List<Alumno> alumnosList = new ArrayList();        

    InputStreamReader isr = new InputStreamReader (is);
    BufferedReader br = new BufferedReader (isr);
    String linea = br.readLine();

    List<int> calificacionesList = null;
    while (linea != null) {
        String[] lineaDividida = linea.split(" "); 

        calificacionesList = new ArrayList<>();
        for (int i = 1; i < lineaDividida.length; i++) {
            calificacionesList.add(Integer.parseInt(lineaDividida[i]));
        }

        alumnosList.add(new Alumno(lineaDividida[0], calificacionesList));

        linea = br.readLine();
    }
    br.close();

    return alumnosList;
}

Now, the cargarAlumnos() method allows you to obtain the data of all the students in a list, which you can then process to obtain any information, such as the average, the highest grade, etc. Note that this method I debugged a bit to adjust it to the new behavior.

    
answered by 18.04.2018 / 05:57
source