Reputation: 85
I want to know why this piece of code gets a compilation error. I'm new to JAVA. I wanted to make a Set of pairs as in C++ What is the meaning of pair in inner class code
import java.io.*;
import java.util.Set;
public class UVa {
public static boolean flag = false;
public static Integer p0, p1;
public static Set<pair<Integer,Integer> > sorter;
public static class pair<first,second>{
public first First;
public second Second;
private pair(first First,second Second){
this.First = First;
this.Second = Second;
}
}
public static void main(String[] args) {
try {
while (true) {
p0 = System.in.read();
p1 = System.in.read();
sorter.add(pair<p0, p1>); //<<Syntax error on token ">", Expression expected after this token
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
Upvotes: 1
Views: 5343
Reputation: 15906
At line no 8:
public static Set<pair<Integer,Integer> > sorter;
Set is not initialize yet that's why is throwing java.lang.NullPointerException
, So for that you have ho initialize it first like following.
public static Set<pair<Integer,Integer> > sorter = new HashSet<UVa.pair<Integer,Integer>>();
And at line no 25
You are try to add pair<p0, p1>
in to set like you are doing
sorter.add(pair<p0, p1>);
But its not a correct syntax . The correct syntax is .
sorter.add(new pair<Integer, Integer>(p0, p1));
or
sorter.add(new pair(p0, p1))
Upvotes: 2
Reputation: 533910
Welcome to Java. While Java is C++, the programming style is a little different. Try the following. Getting pair
to compile is just the first of your problems. ;)
public class UVa {
public static void main(String... args) {
Scanner scanner = new Scanner(System.in);
List<Pair<Integer, Integer>> sorter = new ArrayList<>();
while (scanner.hasNext()) {
int p0 = scanner.nextInt();
int p1 = scanner.nextInt();
sorter.add(Pair.of(p0, p1));
scanner.nextLine();
}
Collections.sort(sorter, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
int cmp = Integer.compare(o1.first, o2.first);
if (cmp == 0)
cmp = Integer.compare(o1.second, o2.second);
return cmp;
}
});
for (Pair<Integer, Integer> pair : sorter) {
System.out.println(pair);
}
}
}
class Pair<First, Second> {
public final First first;
public final Second second;
public Pair(First first, Second second) {
this.first = first;
this.second = second;
}
public static Pair<Integer, Integer> of(int i, int j) {
return new Pair<Integer, Integer>(i, j);
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}
Upvotes: 0
Reputation: 3822
What you want to add to your Set is an object of type pair<Integer, Integer>
. To create one of these you need to use the new
keyword, and call the constructor with the two Integer variables p0
and p1
.
sorter.add(new pair<Integer, Integer>(p0,p1));
It's conventional to name classes with a leading capital. I've rewritten your pair
class below with these conventions, to try and show how it uses Java Generics more clearly:
public static class Pair<T1,T2>{
public T1 first;
public T2 second;
private pair(T1 first, T2 second){
this.first = first;
this.second = second;
}
}
Upvotes: 3
Reputation: 44124
It appears you're trying to construct a new pair
using
pair<p0, p1>
However, the correct syntax is
new pair<Integer, Integer>(p0, p1)
Upvotes: 5
Reputation: 1241
Changed your code :
pair p= new pair<Integer, Integer>(p0, p1);
sorter.add(p); //<<Syntax error on token ">", Expression expected after this token
Upvotes: 1
Reputation: 16119
In your source, pair<p0, p1>
in the statement sorter.add(pair<p0, p1>);
refers to the type pair
. You are not actually creating an object to be added to the set. You might as well have written sorter.add(int);
Upvotes: 0
Reputation: 347332
Shouldn't
sorter.add(pair<p0, p1>);
Read
sorter.add(new pair(p0, p1));
Upvotes: 0