My program is to implement a standalone product search program in Java that lists matching
products for a user who is looking for T-shirts. I am given 3 CSV files. I am trying to keep
the objects of CSV files into a LinkedList
to search for the T-shirts but I am getting cannot
infer type arguments for LinkedList
.
Here is my code in which I am trying to store objects of CSV file into LinkedList
.
package com.example.Mavenassignmentone;
import java.io.*;
import java.util.*;
public class FindTshirt {
List<ShirtModel> shirtlist = new LinkedList<ShirtModel>();
LinkedList<String> list;
showTshirt sh = new showTshirt();
public void find(String filename, String COLOUR, String GENDER_RECOMMENDATION, String SIZE)
throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename));
while (sc.hasNext()) {
String sentence = sc.nextLine().toString();
if (!sentence.isEmpty()) {
StringTokenizer token = new StringTokenizer(sentence, "|");
list = new LinkedList<>(sentence.length());
while (token.hasMoreTokens()) {
list.add(token.nextToken());
}
if (list.get(1).equals(COLOUR) && list.get(2).equals(GENDER_RECOMMENDATION)
&& list.get(3).equals(SIZE)) {
ShirtModel model = new ShirtModel(list.get(0), list.get(1), list.get(2),
list.get(3), list.get(4), Integer.parseInt(list.get(5)),
Float.parseFloat(list.get(6)), list.get(7));
shirtlist.add(model);
}
}
}
}
public void updateView(int choiceCode) {
if (choiceCode == 1) {
Collections.sort(shirtlist, new Comparator<ShirtModel>() {
@Override
public int compare(ShirtModel o1, ShirtModel o2) {
return o1.getPrice() - o2.getPrice();
}
});
}
else if (choiceCode == 2) {
Collections.sort(shirtlist, new Comparator<ShirtModel>() {
public int compare(ShirtModel o1, ShirtModel o2) {
return (int) (o1.getRating() - o2.getRating());
}
});
}
else {
System.out.println("Wrong Choice.");
return;
}
sh.viewtshirt(shirtlist);
}
}
This line of code is giving error:
list=new LinkedList<>(sentence.length());
Error is
cannot infer type arguments
CodePudding user response:
Your problem is in this line of your code.
list = new LinkedList<>(sentence.length());
There is no constructor of class LinkedList that takes an int
parameter.
The line should be:
list = new LinkedList<>();
For the sake of completeness, here is your code with the correction. There is one change and it is marked with the comment CHANGE HERE
.
package com.example.Mavenassignmentone;
import java.io.*;
import java.util.*;
public class FindTshirt {
List<ShirtModel> shirtlist = new LinkedList<ShirtModel>();
LinkedList<String> list;
showTshirt sh = new showTshirt();
public void find(String filename, String COLOUR, String GENDER_RECOMMENDATION, String SIZE)
throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename));
while (sc.hasNext()) {
String sentence = sc.nextLine().toString();
if (!sentence.isEmpty()) {
StringTokenizer token = new StringTokenizer(sentence, "|");
list = new LinkedList<>(); // CHANGE HERE
while (token.hasMoreTokens()) {
list.add(token.nextToken());
}
if (list.get(1).equals(COLOUR) && list.get(2).equals(GENDER_RECOMMENDATION)
&& list.get(3).equals(SIZE)) {
ShirtModel model = new ShirtModel(list.get(0), list.get(1), list.get(2),
list.get(3), list.get(4), Integer.parseInt(list.get(5)),
Float.parseFloat(list.get(6)), list.get(7));
shirtlist.add(model);
}
}
}
}
public void updateView(int choiceCode) {
if (choiceCode == 1) {
Collections.sort(shirtlist, new Comparator<ShirtModel>() {
@Override
public int compare(ShirtModel o1, ShirtModel o2) {
return o1.getPrice() - o2.getPrice();
}
});
}
else if (choiceCode == 2) {
Collections.sort(shirtlist, new Comparator<ShirtModel>() {
public int compare(ShirtModel o1, ShirtModel o2) {
return (int) (o1.getRating() - o2.getRating());
}
});
}
else {
System.out.println("Wrong Choice.");
return;
}
sh.viewtshirt(shirtlist);
}
}
Note that this solves the compiler error you are getting however I did not check whether the code executes correctly since you did not post a reproducible example because your code is missing classes ShirtModel
and showTshirt
.
CodePudding user response:
The list
is declared as a LinkedList
of String
via LinkedList<String>list;
, but list
is initialized as a new linked list with an int
as the constructor argument, but the LinkedList
constructor that you are using accepts a collection as its constructor, not an int
.
This is the reason the compiler cannot infer the type arguments. From the Java type inference doc:
The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally, the inference algorithm tries to find the most specific type that works with all of the arguments.
The easiest fix would be to initialize the list
with the empty LinkedList
constructor:
list = new LinkedList<>();
CodePudding user response:
You have to identify the type of the linked list, change the line to
list=new LinkedList<String>();
and moreover, you in linkedList, you don't have to identify the length.