I am busy with a program that reads in a file in an arrayList and then reads a 2nd file and looks if there are sentences beginning with a word that is in the arrayList.
But now I was wondering.
What can I use better?
An array or an arrayList?
Printable View
I am busy with a program that reads in a file in an arrayList and then reads a 2nd file and looks if there are sentences beginning with a word that is in the arrayList.
But now I was wondering.
What can I use better?
An array or an arrayList?
What do you mean by "better"?
Does your current code work?
Is it easy to understand?
I decided to use an arraylist, only to find a small problem.
I am reading in a file that contains 2 lines.
>HSIBIYG
>OHOUBO7
But he only adds line 1 to the arrayList.
Code:
// Create Scanner for file1.
Scanner x = new Scanner(file1);
// We read the lines one by one in the file.
// Keeps track of the count after something has been found.
int getal = 0;
// Count the number of lines.
if (getal > 0) {
getal++;
}
if (x.hasNextLine()) {
String line = x.nextLine();
// Het ingelezen bestand word geprint.
System.out.println(line);
// ++ adds +1 every line.
getal++;
// We put what is in file1 into an arrayList.
arrayList.add(line);
System.out.println(arrayList);
}
That has nothing to do with ArrayList and everything to do with the fact you are only adding one line to it.
That's an 'if' statement, not a loop, so it'll only execute once.
I first had a while and not an if.
But then he prints my other while loop 2 times.
Well, that code there will only read a single line, and therefore only add a single line to the arraylist.