Hi everybody,
I want to have some kind of a cursor in my program. My goal is: everytime when I write a getName() method I want it to return a next value. But the thing is I don't want to delete any elements befour. So please help me to give an idea how I can achieve it or write some sample.
here is the code:
import java.io.PrintStream;
import java.util.*;
public class Table {
static Set<String> mySet = new LinkedHashSet<String>();
public static void main(String[] args) {
System.out.println("Please enter 3 names");
for(int i=0; i<3; i++)
setName();
System.out.println("");
getName();
getName(); // when I call this and the next method
getName(); // I want to have different output
}
public static void setName() {
Scanner input = new Scanner(System.in);
String str = input.next();
while(!mySet.contains(str)) {
mySet.add(str);
}
}
public static PrintStream getName() {
Iterator<String> itr = mySet.iterator();
String str = itr.next();
return System.out.printf("%s\n",str);
}
}
right now my output looks like this :
Please enter 3 names
Bob
Sam
Ron
Bob
Bob
Bob
But I want it to be like this:
Please enter 3 names
Bob
Sam
Ron
Bob
Sam
Ron