0

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 list.add(s.next()); line gives an error in Eclipse,

The method add(Integer) in the type List<Integer> is not applicable for the arguments (String).

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.

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.

Thanks in advance.


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<Integer> 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);
        }
    }

}
1
  • 6
    Scanner#next() returns a String, you want Scanner#nextInt(). This will also require some more specific file handling (hasNextInt()) and skipping new lines depending on your format of your txt file. Commented May 6, 2013 at 14:31

3 Answers 3

3

s.next() refers to a method that returns a String type. Since Java is strongly typed, an integer or int type must be returned from the user. s.nextInt() will work just fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the help, that did the trick! I think I have had my head buried in the books for too long and just got burned out. Thanks for helping me catch the obvious!
1

You are trying to add a String to a list of Integers.

s.next() returns the next token as a string, which cannot be added to a list of integer obviously.

1 Comment

Thanks for all the help, that did the trick! I think I have had my head buried in the books for too long and just got burned out. Thanks for helping me catch the obvious!
1

try this:

    List<Integer> list = new ArrayList<Integer>();

    //---- ArrayList 'list'
    Scanner s = new Scanner(new File("C:/Users/emissary/Desktop/workspace/stuff/src/numbers.txt"));

    while   (s.hasNextInt())   {
        list.add(s.nextInt());
    }
    s.close();

    Collections.sort(list);
    for (Integer integer : list){
        System.out.printf("%s, ", integer);
    }

s.hasNextInt() checks if there is an integer in the next data from scanner. And to add integer into a list of integer you must use nextInt that return an integer but not a string Sorry for my bad english

1 Comment

Thanks for all the help, that did the trick! I think I have had my head buried in the books for too long and just got burned out. Thanks for helping me catch the obvious!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.