Reputation: 3
Can you help me with this? I don't have any idea how to fix my code. I want to get the index from IDnumber using binarySearch from collections then I will use that IDnumber's index to get the element from the Names but Im having a problem.
I got some errors when Im running it but I dont see any errors here in NetBeans.
Here is the whole code.
public class Attendance {
public static void main(String[] args){
List <Integer> IDnumber = Arrays.asList(121,122,123,124,125);
List <String> Names = Arrays.asList("Victor","Arvin","Marthie","Mariam","Argel");
System.out.println("Log In");
System.out.println("Enter your Student number : ");
Scanner scanner = new Scanner(System.in);
int StudentNumber = scanner.nextInt();
int x = StudentNumber;
String s = Names.get(x);
System.out.println(Collections.binarySearch(IDnumber,x));
System.out.println(" Student Name : " + s);
}
}
Can I pass the Int index to String?
Upvotes: 0
Views: 102
Reputation: 3058
You probably want:
int index = Names.indexOf(StudentNumber);
Once you have this you know the index for both the ID and the name; you don't need to do the binary search (which depends your list of IDs staying in order, and will break if not). (Binary search would be faster - but the gain is insignificant unless your list is huge.)
Or do you actually want them to type in 0, 1, 2, (etc) and not 121, 122, 123, (etc)?
BTW. Please use Java conventions - e.g. variables should be in camel case (idNumbers, names, thisIsAVariable, etc).
Upvotes: 0
Reputation: 1028
If the user enters their student number (say, 121), you're trying to do this:
String s = Names.get(121);
When there is no value at the index 121 in Names
. Names
is only 5 elements long. What you want to do is look up the student number in IDnumber
list, then use that index of the ID as the index into Names
.
Try something like:
int idIndex = IDnumber.indexOf(x);
String s = Names.get(idIndex);
Although you might consider using a Map to store the relationship between the Name and the ID. What you're doing here heavily depends on the order of the IDs and Names - any shift in the ordering and your relationship is lost.
Upvotes: 1
Reputation: 6715
I think you want a Map:
private static final Map<Integer, String> STUDENTS;
static {
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(Integer.valueOf(121), "Victor");
m.put(Integer.valueOf(122), "Arvin");
m.put(Integer.valueOf(123), "Marthie");
m.put(Integer.valueOf(124), "Mariam");
m.put(Integer.valueOf(125), "Argel");
STUDENTS = Collections.unmodifiableMap(m);
}
then...
String s = STUDENTS.get(scanner.nextInt());
Upvotes: 1
Reputation: 748
You should sort the Collection before you can do a binarysearch on it: Use:
Collections.sort( IDnumber );
System.out.println(Collections.binarySearch(IDnumber,x));
Upvotes: 0
Reputation: 2243
you get the value from the list of Names which contains only index of 4 .
Hence values after 4 will give java.lang.ArrayIndexOutOfBoundsException
Upvotes: 0
Reputation: 8715
The error is to assign StudentNumber
to x
. Instead you should do
int x = Collections.binarySearch(IDnumber, StudentNumber)
Then you will get the index of the student with the number entered (121, for example).
Then you get the name of the student like you do
String s = Names.get(x);
Upvotes: 2