Saturday, 14 September 2013

How do I get Java to output unique integers from a command-line argument?

How do I get Java to output unique integers from a command-line argument?

The code below checks to ensure the command-line arguments are integers?
However, I need it to only print each integer in the command line once,
regardless of how many times that integer appears in the command line. How
do I do that?
public class Unique {
public static void main(String[] args) {
for (int i = 0; i<args.length; i++) {
try {
int j = Integer.parseInt(args[i]);
System.out.println(j);
}
catch (NumberFormatException e) {
System.out.println("Your command line argument "+ args[i] +"
is a non-integer!");
}
}
}
}

No comments:

Post a Comment