0

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).

10
  • 4
    Please always post the complete error message, word-for-word with your question. Commented Sep 14, 2015 at 20:30
  • Note that read.next() always returns a String. There are never going to be any Integer objects in that list. Commented Sep 14, 2015 at 20:32
  • 1
    This code doesn't make much sense. Scanner.next() returns a String, not an E. So the list will never contain any Integer, and the method should return a List<String>. You shouldn't ignore the compiler warnings. They tell you that your code is wrong. Commented Sep 14, 2015 at 20:33
  • Is there a way then to tell if it is an Integer, I am stuck on how to properly get that to work. Commented Sep 14, 2015 at 20:34
  • 1
    It is NEVER an Integer. next() returns a String. I have no idea of what you're trying to achieve here. Commented Sep 14, 2015 at 20:35

2 Answers 2

3

As others pointed out, your list will never contain an Integer, because next() returns a String.

Given your last comment:

I am trying to be able to remove ints from a text file, only leaving strings left. Say for example I had a text file that said "A B C 1 2 3", first the Scanner (I need to use a scanner) would take in the file, and put it into an ArrayList. Then when I use the remove method it would take out all values that are Integers, and leave alone the Strings. The final output at the end would be "A B C".

Don't first load them as Integer and then remove them. Instead, don't load them:

List<String> list = new ArrayList<>();
try (Scanner sc = new Scanner("A B C 1 2 3")) {
    while (sc.hasNext()) {
        if (sc.hasNextInt())
            sc.nextInt(); // get and discard
        else
            list.add(sc.next());
    }
}
System.out.println(list);

Output

[A, B, C]
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a reason it is outputting this first, and then what is in the text file? [{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170, {\fonttbl\f0\fswiss\fcharset0, Helvetica;}, {\colortbl;\red255\green255\blue255;}, \margl1440\margr1440\vieww10800\viewh8400\viewkind0, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural, \f0\fs24, \cf0,
@FyreeW Your file was saved as an RTF file. Save it as a plain text file.
Changed it to a .txt file,worked after that. Thank you @Andreas for pointing that out.
2

Change the signature of removeInts to be generic:

public <E> void removeInts(ArrayList<E> fileList)

2 Comments

Wow that was simple. Thanks for the help.
I am happy to help! Please accept the answer if it solved it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.