Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

440
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!