Note that there are some explanatory texts on larger screens.

plurals
  1. POlist.add not working with ArrayList
    primarykey
    data
    text
    <p>I am trying to add integers from a file into an ArrayList and list.add doesn't want to work. I've only tried about a thousand different ways to write this code. The <code>list.add(s.next());</code> line gives an error in Eclipse, </p> <blockquote> <p><code>The method add(Integer) in the type List&lt;Integer&gt; is not applicable for the arguments (String).</code> </p> </blockquote> <p>Sounds like I am somehow trying to do something with an integer that can only be done with a string, but I need them to remain integers and if I hadn't been searching, studying and cramming my head with Java for the last 5 days straight I could probably understand what it means.</p> <p>I can get it to work with a regular array just fine, but my ArrayList collection is a real pain and I'm not sure what I'm doing wrong. Any help would be much appreciated. </p> <p>Thanks in advance.</p> <hr> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class MyCollection { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { List&lt;Integer&gt; list = new ArrayList(); //---- ArrayList 'list' Scanner s = new Scanner(new File("C:/Users/emissary/Desktop/workspace/stuff/src/numbers.txt")); while (s.hasNext()) { list.add(s.next()); } s.close(); Collections.sort(list); for (Integer integer : list){ System.out.printf("%s, ", integer); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload