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());
}
}
}
At line no 8:
Set is not initialize yet that’s why is throwing
java.lang.NullPointerException, So for that you have ho initialize it first like following.And at line no 25
You are try to add
pair<p0, p1>in to set like you are doingBut its not a correct syntax . The correct syntax is .
or