Results 1 to 4 of 4
  1. #1
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default File Reader with multiple files

    Currently I am trying to program a simple Artificial Intelligence by using a series of text files for the user name login, and the questions being asked. Currently, I am trying to set it so you can only pick one of four questions via a drop down menu in the Java GUI builder. I want it so I only have to use one BufferedReader for all of the files, so I put them in an Array like this...

    Java Code:
    public int[] QuestionLines() throws IOException
        {
        String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt",
        "Say something random.txt"};
        String Line;   
    
        for(i = 0; i < 4; i ++)
        {
        FileReader NameReader = new FileReader(filepath[i]);
        BufferedReader reader = new BufferedReader(NameReader);
        } 
        }
    I also have two methods that get the number of lines for a text file and stores the Strings in the file into an ArrayList...

    Java Code:
    public ArrayList<String> FileData() throws IOException
        {
        FileReader NameReader = new FileReader("UserNames.txt");
        BufferedReader linereader = new BufferedReader(NameReader);
        numberoflines = ReadLines();
    
        ArrayList<String> userdata = new ArrayList<String>();
        int c;    
    
        for(c=0; c < numberoflines; c++)
        {
        if(userdata != null)
            {
            userdata.add(c, "User" + (c+1));
            userdata.set(c, linereader.readLine());
            }
        }
        linereader.close();
        return userdata;
        }
        //-------------------------------------------------------------------------
        public int ReadLines() throws FileNotFoundException, IOException
        {
        FileReader NameReader = new FileReader("UserNames.txt");
        BufferedReader reader = new BufferedReader(NameReader);
    
        String Line;
        numberoflines = 0;
    
        while ((Line = reader.readLine()) != null)
        {
        numberoflines++;
        }
        reader.close();
        return numberoflines;
        }
    I was wondering if there was a way to do the same thing but with the multiple files in one reader with the Array for the file names. This is what I have come up with so far but have not been able to test it, being that it is not yet used in my program...

    Java Code:
    public ArrayList<String> QuestionData() throws IOException
        {
        String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt","Say something random.txt"};
        ArrayList<String> questiondata = new ArrayList<String>();
        int c;
    
        for(i = 0; i < 4; i ++)
        {
        FileReader NameReader = new FileReader(filepath[i]);
        BufferedReader linereader = new BufferedReader(NameReader);
        for(c=0; c < questionlines[i]; c++)
        {
        if(questiondata != null)
            {
            questiondata.add(c, "Answer" + (c+1));
            questiondata.set(c, linereader.readLine());
            }
        linereader.close();
        }
        }
        return questiondata;
        }
        //-------------------------------------------------------------------------
        public int[] QuestionLines() throws IOException
        {
        String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt",
        "Say something random.txt"};
        String Line;   
    
        for(i = 0; i < 4; i ++)
        {
        FileReader NameReader = new FileReader(filepath[i]);
        BufferedReader reader = new BufferedReader(NameReader);
        while ((Line = reader.readLine()) != null)
        {
        questionlines[i]++;
        }
        reader.close();
        }
        return questionlines;
        }
    }
    If anyone has any advice or ideas that you can give me, it would be most appreciated. Thanks ^.^

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    This is not using a single reader:
    Java Code:
        for(i = 0; i < 4; i ++)
        {
            FileReader NameReader = new FileReader(filepath[i]);
            BufferedReader reader = new BufferedReader(NameReader);
        }
    It is creating a new reader for each file, which it has to do since a reader is for a specific thing, generally.

    That aside, I'm having problems actually reading your code, since the formatting's all screwy.

  3. #3
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    I meant you only had to use the Reader once, I realize there are multiple readers created, but only one coded. And sorry for the formatting, I took it right out of a text document, since not all of the computers here at school have NetBeans on them that was the only way to actually get the code.

    Anyway, I guess that I'm on the right track? I was just wondering if there was an easier way to do it, cause honestly, I'm just typing away only half knowing if what I'm doing is right because I can't check it in my code yet. Well, that's pretty much how I've gotten most of my code so far... >.>;

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Since I'm not too clear on the requirements all I can say is "does this do what you want".

Similar Threads

  1. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 02:06 AM
  2. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 08:19 AM
  3. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 07:43 PM
  4. Opeing multiple pdf files in different acrobat reader windows
    By shweta.ahuja in forum Web Frameworks
    Replies: 2
    Last Post: 05-07-2008, 12:33 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •