Search code examples
javastack-overflow

Reading Strings from text files in java


im studying for my programming final exam. I have to write a program which opens a file which is stored in the string fileName and look in the file for a String called personName and this should print the first string after personName then the program should terminate after printing it, if the argument personName is not in the file then it should print "this name doen't exsit" then if an IOException occurs it should then print "there is an IO Error" and the program should exsit using system.exit(0)

the program should use the file info.txt and each line should contain two strings first string name and second age.

everything must be in one method

data.txt contains

Max 60.0

joe 19.0

ali 20.0

my code for this so far is :

public class Files{

    public void InfoReader(String fileName, String personName)
    {

      try{
         try{
                   // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("C://rest//data.txt");
                  // Get the object of DataInputStream

                  DataInputStream in = new DataInputStream(fstream);
                  BufferedReader br = new BufferedReader(new InputStreamReader(in));

                  //Read File Line By Line
                  while ((fileName = br.readLine()) != null) {
                      // Print the content on the console
                      (new Files()).infoReader("info.txt","Joe"); //this prints the age
                  }
                  //Close the input stream
                  in.close();
              }

              catch (IOException e)
              {//Catch exception if any
                    System.out.println(" there is an IO Error");
                    System.exit(0);
              }
     }
    catch (Exception e)
              {//Catch exception if any
                    System.out.println("that name doesn't exists");

              }
    }
}

infoReader(info.txt,Joe); should print 19.0
But I am getting a java.lang.StackOverflowError

any help would be much appreciated!!

Thanks in advance!


Solution

  • This is what I think you are trying to do. And if doesn't, at least can work as an example. Just as amit mentions, your current error is because of the recursive call, which I think is not necessary.

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Files {
    
        public void InfoReader(String fileName, String personName) {
            try {
                // Open the file that is the first command line parameter
                FileInputStream fstream = new FileInputStream(fileName);
    
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
                String line = null;
    
                //Loop until there are no more lines in the file
                while((line = br.readLine()) != null) {
                    //Split the line to get 'personaName' and 'age'.
                    String[] lineParts = line.split(" ");
    
                    //Compare this line personName with the one provided
                    if(lineParts[0].equals(personName)) {
                        //Print age
                        System.out.println(lineParts[1]);
                        br.close();
                        System.exit(0);
                    }
                }
    
                br.close();
                //If we got here, it means that personName was not found in the file.
                System.out.println("that name doesn't exists");
            } catch (IOException e) {
                System.out.println(" there is an IO Error");
            }
        }
    }