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 error:
SortingWords.java:73: error: cannot find symbol
array.add(element);
^
symbol: method add(String) location: variable array of type String[]
I have checked my spelling and all seems to be okay. What is happening and how can I fix it?
public class SortingWords {
//global variables
public static int count = 0;
public static void main(String [ ] commandlineArguments){
String[ ] array = readFileReturnWords(commandlineArguments[0]);
sortAndPrintArray(array, commandlineArguments[0]);
if (commandlineArguments.length != 1) {
System.out.println("Please enter the INPUT file name as the 1st commandline argument.");
System.out.println("Please enter exactly one (1) commandline arguments.");
// Immediately terminates program
System.exit(1);
}// end of if
// if no commandline argument errors, continue program
String inputFile = commandlineArguments[0];
}
/**
* readFileReturnWords - Used To Read from File & Store Each Word in Array
*
* @param inputFile is the name of the file
*/
public static String [] readFileReturnWords(String inputFile){
//create array
//read one word at a time from file and store in array
//return the array
//error checking for commandline input
//make an array of Strings
final Integer MAX = 100;
String array[] = new String [MAX];
String element = "";
// read items from file & store in array
//connects to file
File file = new File(inputFile);
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
// Print error message.
System.out.print("ERROR: File not found for " + inputFile);
}
// if made connection to file, read from file
if (scanFile != null) {
String firstLineOfFile = "";
firstLineOfFile = scanFile.nextLine();
// keeps looping if file has more lines..
while (scanFile.hasNextLine()) {
// get a line of text..
String line = scanFile.nextLine();
// divides each line by commas
Scanner lineInput = new Scanner(line).useDelimiter("\\s*");
element = lineInput.next();
array.add(element);
count ++;
}
}
System.out.println("Alphabetical (ASCII) listing of words in file: ");
System.out.println("Index Element");
for(int i=0;i<MAX;i++){
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(array[i]);
}
return array;
}
Much Thanks