0

I apologize in advance if the solution is relatively obvious; however, the AP compsci curriculum at my highschool included practically no involvement with IO or file components. I've been trying to write an elementary flashcard program - thus it would be much more practical to read strings off a text file than add 100 objects to an array. My issue is that when I go to check the size and contents of the ArrayList at the end, it's empty. My source code is as follows:

public class IOReader
{
  static ArrayList<FlashCard> cards = new ArrayList<FlashCard>();
  static File file = new File("temp.txt");

  public static void fillArray() throws FileNotFoundException, IOException
  {
    FileInputStream fiStream = new FileInputStream(file);
    if(file.exists())
    {
      try( BufferedReader br = new BufferedReader( new InputStreamReader(fiStream) )
      {
        String line;
        String[] seperated;
        while( (line = br.readLine()) != null)
        {
          try
          {
            seperated = line.split(":");
            String foreign = seperated[0];
            String english = seperated[1];
            cards.add( new FlashCard(foreign, english) );
            System.out.println(foreign + " : " + english);
          }
          catch(NumberFormatException | NullPointerException | ArrayIndexOutOfBoundsException e)
          {
            e.printStackTrace();
          }
          finally{ 
            br.close(); 
          }
        }
      }
    }
    else{
      System.err.print("File not found");
      throw new FileNotFoundException();
    }
  }
  public static void main(String[] args)
  {
    try{
      fillArray();
    }
    catch (Exception e){}
    for(FlashCard card: cards)
      System.out.println( card.toString() );
    System.out.print( cards.size() );
  }
}

My text file looks as thus:

Volare : To Fly
Velle : To Wish
Facere : To Do / Make
Trahere : To Spin / Drag
Odisse : To Hate
... et alia

My FlashCard class is very simplistic; it merely takes two Strings as parameter. The issue though is that the output whenever I run this is that nothing is printed except for the 0 printed in the main method, indicating that the ArrayList is empty. I thank you in advance for any help, as any would be appreciated.

12
  • 2
    You may be closing your bufferedreader too early. You're closing it as a finally after the try-catch construction. However, this means that on the second iteration of the while loop the bufferedreader is already closed. Close it after you complete the while loop. This won't totally solve the problem though, as it doesn't explain why nothing at all is printed from the loop. Commented Aug 7, 2014 at 23:07
  • 1
    First impresion: Instead of ignoring the exception e in main(), try printing the stack trace. You may be getting some exception that isn't caught by one of your other catch blocks. Commented Aug 7, 2014 at 23:07
  • Mshnik is right: br.close() is inside the while loop and therefore will be called for each iteration of the while loop, which isn't what you want. Commented Aug 7, 2014 at 23:09
  • I'm guessing it can't find the file. The problem is that new FileInputStream will throw an exception (which your main program then catches and ignores), before you get to file.exists(). Commented Aug 7, 2014 at 23:18
  • @Chris Dare , I got the size of the arraylist lemme know if you wanna I can post up my answer and I can explain to you what is going on Commented Aug 8, 2014 at 0:29

1 Answer 1

1

Some point to consider:

  1. in your fillArray() is good to throws exceptions and caught them inside agent portion of your program which is main(), so your code in fillArray() will be more readable and you will not hide the exceptions.
  2. I do not think there is any need to check whether the file exist because if it does not exist, the exception will be throw and main() function will be use it.
  3. I use Igal class instead of FlashCard class which is as same as your FlashCard class

Code for Igal Class:

public class Igal {
    private String st1;
    private String st2;

public Igal(String s1, String s2){
    st1 = s1;
    st2 = s2;
}



/**
 * @return the st1
 */
public String getSt1() {
    return st1;
}

/**
 * @param st1 the st1 to set
 */
public void setSt1(String st1) {
    this.st1 = st1;
}

/**
 * @return the st2
 */
public String getSt2() {
    return st2;
}

/**
 * @param st2 the st2 to set
 */
public void setSt2(String st2) {
    this.st2 = st2;
}

@Override
public String toString(){
    return getSt1() + " " + getSt2();
}

}

Code:

static List<Igal> cards = new ArrayList<>();
static File file = new File("C:\\Users\\xxx\\Documents\\NetBeansProjects\\Dictionary\\src\\temp.txt");

public static void fillArray() throws FileNotFoundException, IOException {

        FileInputStream fiStream = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fiStream));
        String line;
        String[] seperated;

        while ((line = br.readLine()) != null) {
            seperated = line.split(":");
            String foreign = seperated[0];
            String english = seperated[1];
            System.out.println(foreign + " : " + english);
           Igal fc = new Igal(foreign, english);
           cards.add(fc);
      }
 }

public static void main(String[] args) {
    try {
        fillArray();
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("------------------");
    for (Igal card : cards) {
        System.out.println(card.toString());
    }
     System.out.print("the size is " + cards.size()+"\n");

temp.txt content are as follows

Volare : To Fly
Velle : To Wish
Facere : To Do / Make
Trahere : To Spin / Drag
Odisse : To Hate

output:

------------------
Volare   To Fly
Velle   To Wish
Facere   To Do / Make
Trahere   To Spin / Drag
Odisse   To Hate
the size is 5
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this fixed the errors I came across during run-time :-). Programs running the way it should now!
@ChrisDare if my answer helped, can you lemme know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.