I'm trying to develop a small android app and I ran into this rather "weird" problem, I need to compare two strings and do few things if they match, I used == operator first, and even if the 2 strings were the same, my if statement treats them as not equal and just moves on to next statements (I know the two strings are equal because I watch their values and debug step-by-step). So I tried to use .equals operator instead, but it's giving me StringIndexOutOfBoundsException on the previous two statements which are:
String s1 = s.substring(0, s.indexOf("\t"));
String s2 = subjectName.substring(0, subjectName.indexOf("\t"));
Here is a screenshot of step-by-step debugging, both s1 and s2 has a value "Mathematics" but the if statement ignores it. enter image description here
Here is a more "complete" code:
if(marks.size() > 0)
{
for(Student student: students)
{
ArrayList<String> subjectNames = new ArrayList<>();
String studentFullName = student.getSurName() + "\t" + student.getName();
studentNames.add(studentFullName);
for(Subject subject: subjects)
{
String subjectName = subject.getName() + "\t" + 0;
subjectNames.add(subjectName);
}
markListHashMap.put(studentFullName, subjectNames);
}
for(Mark mark: marks)
{
Student student = dbHelper.getStudent(mark.getStudentRegister());
String fullName = student.getSurName() + "\t" + student.getName();
Subject subject = dbHelper.getSubject(mark.getSubjectId());
String subjectName = subject.getName() + "\t";
subjectName = subjectName + dbHelper.getTotal(mark);
ArrayList<String> subjects = (ArrayList<String>) markListHashMap.get(fullName);
int index = 0;
for(String s: subjects)
{
String s1 = s.substring(0, s.indexOf("\t"));
String s2 = subjectName.substring(0, subjectName.indexOf("\t"));
if(s1 == s2)
{
s = s.substring(0, s.indexOf("\t"));
s = s + subjectName.substring(subjectName.indexOf("\t") + 1, subjectName.length());
markListHashMap.get(fullName).set(index, s);
}
else
index++;
}
}