So right now I am getting an Syntax error when I am trying to use a method that does not return any value (is void), and that takes in arguments ArrayList<E> fileList. My goal is to take in a text file that has both String, and Integer objects, and in the remove method if it finds a Integer then it will be removed from the list. This is so it will only be leaving the Strings at the end. Here is the code that shows both the reading of the file, and the removeInts method I am trying to use:
@SuppressWarnings("unchecked") //IDE told me to add this for adding objects to the ArrayList
public <E> ArrayList<E> readFile(){
ArrayList<E> fileList = new ArrayList<E>();
try {
Scanner read = new Scanner(file); //would not let me do this without error handling
while(read.hasNext()){ //while there is still stuff to add it will keep filling up the ArrayList
fileList.add((E)read.next());
}
} catch (FileNotFoundException e) {
System.out.println("File not found!");
e.printStackTrace();
}
removeInts(fileList);
return fileList;
}
public void removeInts(ArrayList<E> fileList){
for(int i = 0; i < fileList.size(); i++){
if(fileList.get(i) instanceof Integer){
fileList.remove(i);
}
else{
//does nothing, does not remove the object if it is a string
}
}
I am getting the syntax error at removeInts(fileList).
read.next()always returns aString. There are never going to be anyIntegerobjects in that list.