I have to read from a file and then place the words that are found into a ArrayList.
public boolean load(String filename)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("wordlist.txt")));
String line = null;
while ((line = br.readLine()) != null)
{
String[] values = line.split("\n");
wordlist.add(new WordPair(answer, question));
}
} catch (IOException ioe)
{
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ioe);
}
return false;
}
Below is the tester code I have used:
public void testLoad() {
assertTrue(wordPairDemo.save(fileName));
wordPairDemo.clear();
assertTrue(wordPairDemo.load(fileName));
assertTrue(wordPairDemo.size() == 3);
assertEquals(wordPairDemo.lookup("hest"),"horse");
assertEquals(wordPairDemo.lookup("hus"), "house");
assertEquals(wordPairDemo.lookup("båd"),"boat");
}
I get the junit.Framework.AssertionFailedError, and I am not sure what is causing it. I have tried several different types of ways of doing this, through scanners, etc. but I always get this error, and I am not sure what to do with it.
Any help appreciated! :)
As per your example in the comment below, the file contains pairs in single line: horse,hesthouse,husboat,båd
you should split according to the actual delimiter you're using - ,
. Eg:
public boolean load(String filename)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("wordlist.txt")));
String line = null;
// I'm leaving this loop under the assumption that there may be multiple lines
while ((line = br.readLine()) != null)
{
String[] values = line.split(",");
for (int i = 0; i < line.lenght() / 2; ++i) {
wordlist.add(new WordPair(values[i], values[i + 1));
}
}
} catch (IOException ioe)
{
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ioe);
}
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.