Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

446
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda