How to read from a file and save to variables in Java

Asked

Viewed 1,606 times

3

I’m having difficulties, because in a file manipulation exercise in Java, the teacher asked us to create a program that takes the name and note 1 and note 2 of two proofs and store in a txt file as follows: name;Nota1;nota2 only that later in another exercise he asks to read from the file the data and calculate the media of the notes saving again the data in another file in the following form name;Nota1;nota2;media, as I do this reading and save the data in variable String,float, float again ???

public class LeitorComun extends Leitor {
    private String nome;
    private  double n1,n2,media;
    String linha;

    public LeitorComun(FileInputStream arquivo) {
        super(arquivo);
    }


     @Override
    public void ler() throws IOException{
        InputStreamReader isr = new InputStreamReader(this.arquivo);
        BufferedReader br = new BufferedReader(isr);
        do{
        this.linha = br.readLine();
              if(this.linha != null){
                  String [] palavras = this.linha.split(";");
                  System.out.println("nova linha ------------------------------------");
                  for(int i =0; i<palavras.length;i++){
                      System.out.println("palavra lida: "+ palavras[i]);
                  }
              }
          }while(this.linha != null);

    }
}
  • 3

    What have you tried to do? Add to question.

  • This 'file' you want to read is a . txt? was very confusing your question. Maybe to answer your question, you can use a String[3] and convert notes 1 and 2 with parseint.

  • It is for in the second problem I have to read from the.txt file I created, it has the name and the notes, Ex: Marco;11.5;22.3, as I will read the name and the notes and put in the variables name,Nota1,nota2, being that when reading by Inputstreamreader and Bufferedreader they read the whole line, how I will separate the data ???

1 answer

1


You can create a simple method for converting String for double:

private double converter(String texto) {
  return Double.parseDouble(texto);
}

And change your method ler for:

@Override
public void ler() throws IOException {
  InputStreamReader isr = new InputStreamReader(this.arquivo);
  BufferedReader br = new BufferedReader(isr);

  do {
    this.linha = br.readLine();

    if (this.linha != null) {
      double nota1;
      double nota2;
      double media;

      try {
        String[] palavras = this.linha.split(";");
        System.out.println("nova linha ------------------------------------");
        nota1 = this.converter(palavras[1]);
        nota2 = this.converter(palavras[2]);
        media = (nota1 + nota2) / 2;
        System.out.println(palavras[0] + "/ Média: " + media);
      } catch (NumberFormatException ex) {
        System.out.println("A linha \"" + this.linha + "\" contém erros");
      }
    }
  } while (this.linha != null);
}

With the following file notas.txt:

Joseph;10;9

Andrew;5;8

The result is:

nova linha ------------------------------------
José/ Média: 9.5
nova linha ------------------------------------
André/ Média: 6.5
  • Thank you very much, now I understand how it works. The split type that breaks and separates the strings when it finds " ; "ai later is just convert.

  • @Exact Marcoantoniogomes :) Don’t forget to choose a reply that suits you and mark it so it can be used if someone has a similar question!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.