0
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

class Arralist{

public static <Cd> void main(String [] args) {

ArrayList<Cd> liste = new ArrayList<Cd>();

try {
Scanner input = new Scanner(new File("text.txt"));

while (input.hasNext()) {
liste.add(input.next());
}
input.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

I got error like this :

: error: no suitable method found for add(String)
liste.add(input.next());
^
method ArrayList.add(int,Cd) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(Cd) is not applicable
(actual argument String cannot be converted to Cd by method invocation conversion)
where Cd is a type-variable:
Cd extends Object declared in method <Cd>main(String[])
1 error

Process completed.


What wrong how do I fix this

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Taywin
0

The reason is that the "Cd" class is not a String class. the next() method return a String if I remember correctly. What is your Cd constructor? Possible solution (depending on your Cd constructor) is as follows...

liste.add(new Cd(input.next()));

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.