I have a program that reads from a file, takes each word and adds it to an array as a String. I\'m having a bit of trouble with adding the Strings to the array. I get the e
Use an ArrayList
instead of an array:
List array = new ArrayList();
while (scanFile.hasNextLine()) {
String line = scanFile.nextLine();
Scanner lineInput = new Scanner(line).useDelimiter("\\s*");
element = lineInput.next();
// the add() method is available for Collections but
// not for primitive arrays
array.add(element);
count++;
}
for (String element : array) {
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(element);
}