Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

441
Visualizações
How to read from textfile and pass values to another class Java?

I'm making a Java program that needs to read info from a text file and then store it in an array and pass it to another class when called. My issue is that I can't seem to call it due to the IOException needed in the file reader class.

This is the main class that is supposed to call the fileReader.

public class window {
public static void main(String[] args){
    String[] people = readFromText.read("people.txt");
    }
}

File Reader Class

public class readFromText{
    public static String[] read(String textFile) throws IOException {
      BufferedReader inputFile = new BufferedReader(new 
      FileReader(textFile));
      String[] array = new String[10];
      String line = inputFile.readLine().toString();
      int cnt = 0;
      while (line!=null){
        array[cnt] = line;
        line = inputFile.readLine().toString();
        cnt++;
      }
      inputFile.close();
      return array;
   }
}

Is it possible to do this, this way?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Firstly your code is not correct. You can not return the String[] array for the function need String[][]. Secondly for problem about exception you just need to catch it in your main class.

try {
    String[] people = readFromText.read("people.txt");
} catch (IOException e) {           
    e.printStackTrace();
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda