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...
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...
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...
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 ^.^