My class only covered binary searching ints, which is much easier it seems. My assignment is searching Strings.
My JSON file is an already sorted list (in alphabetical order) of 20 student names and grades. I'll show one:
"Students": [
{
"name": "Daniel",
"id": "144586754",
"course works":[
{
"name": "q1",
"score": 9
},{
"name": "q2",
"score": 5
},{
"name": "q3",
"score": 7
},{
"name": "midterm",
"score": 79
},{
"name": "final",
"score": 57
}
]
This is my attempt at my binary search:
System.out.print("Enter students name you want to find: ");
String nameInput = command.next();
student = new Student(nameInput);
JsonArray studentsArr = jsonObject.getJsonArray("Students");
for (JsonValue value : studentsArr) {
int first = 1;
int last = studentsArr.size();
int mid;
while (first < last) {
mid = first + last / 2;
if (nameInput.compareTo(studentsArr.getString(mid)) > mid) {
first = mid + 1;
} else {
last = mid;
}
student.identify();
}
if (nameInput.compareTo(studentsArr.getString(first)) == first) {
student.identify();
}
}
This is from trying to work off the outline from notes, and I have tried looking stuff up but do not understand how this can work with strings.