0

I'm trying to read a .txt file that contains several lines with the name of a professional carreer in each one of them. I've created a Scanner but whenever I want to add what the scanner have just read and try to add it to the arrayList, this error pops up

The method add(ClassName) in the type ArrayList is not applicable for the arguments (String)

ArrayList<Claseqla> clista = new ArrayList<Claseqla>();

        Scanner s = new Scanner(new File("texto.txt"));

        while(s.hasNextLine())
        {
            **clista.add(s.nextLine());**
        }

This is the piece of code inside another class; The bold marked line is where the error pops up. clista only has 2 attributes but I'd like to add them to the list with just one String element filled and the other empty (Is that even possible?)

2 Answers2

3

s.nextLine() returns a String. But your ArrayList has generic type Claseqla. You need to create a Claseqla object using the string you grab from the s.nextLine() call, and then add that object to your ArrayList.

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
  • Thanks! It worked, now the only problem I have is that when I print the whiole list like System.out.println(clista); the display looks like the adress from the Claseqla values. – Joel Flores Sottile Mar 30 '16 at 03:52
  • I'm getting the same amount of elements as in the text printed, but they look like adresses. How can I print the real value of the string in that ArrayList? (thanks again!) – Joel Flores Sottile Mar 30 '16 at 03:53
1

I guess you are trying to say that the class Claseqla has 2 attributes. If so, then you can create a Claseqla object and set the value of one of the attributes with s.nextline()

while(s.hasNextLine())
{
    Claseqla cq = new Claseqla();
    cq.setCareer(s.nextLine());
    clista.add(cq);
}

This is asumming that you have an attribute named career (String) in your Claseqla class with its respective setter function.

Rafael
  • 555
  • 6
  • 19