-2

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++;
            }
        }
Taivna
  • 15
  • 4
  • 3
    If `s.substring(0, s.indexOf("\t"))` gives you an index error, it is because your string does not contain `\t`. It is not **caused by** the equals operator. – khelwood Apr 20 '17 at 11:22
  • s does contain "\t" because I also watch it's value while debugging step by step and you can see that in the statement String subjectName = subject.getName() + "\t" + 0; I did add "\t" to it. – Taivna Apr 20 '17 at 11:47
  • Then it's a different line that is throwing the exception. – khelwood Apr 20 '17 at 11:50
  • How can it give you `StringIndexOutOfBoundsException` on the previous two lines? If it gives SIOOBE on the first line the second line will not be reached - if it gives SIOOBE on the second line it cannot have happened on the first line... – Thomas Kläger Apr 20 '17 at 12:50
  • It's giving me SIOOBE on the first line if I use .equals to compare s1 and s2 and If I use == operator I don't get any exception, but the if statement body is not executed even if the 2 strings are equal, they have the same length and same value "Mathematics" – Taivna Apr 20 '17 at 13:01

3 Answers3

1

You are getting StringIndexOutOfBoundsException

Thrown by String methods to indicate that an index is either negative or greater than the size of the string.

Problem coming from

s.substring(0, s.indexOf("\t")); //this line
  1. Make sure your s having \t
  2. s having Length() and not null .

.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Please make sure you s1 and s2 is not null and having length>=0 add below line before comparing both variables (s1 and s2)

if(s1!=null && s2!=null && s1.length()>=0 && s2.length>=0){
  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++;
  }
Arsalan Khan
  • 403
  • 3
  • 11
  • I see both s1 and s2 values become "Mathematics" before the if statement while debugging step-by-step and watching their values, but the if statement ignores it as if they are not equal, that's why I wrote "a weird problem" – Taivna Apr 20 '17 at 11:50
0

The condition s1 == s2 will never be true, since the two lines:

String s1 = s.substring(0, s.indexOf("\t"));
String s2 = subjectName.substring(0, subjectName.indexOf("\t"));

create different String objects. These String objects may appear to be equal (i.e. contain the same letters), but == just compares object references. See How do I compare strings in Java?


The reason for the StringIndexOutOfBoundsException is in the way that you create your resulting string if the condition is fullfilled (i.e. if you do s1.equals(s2)):

s = s.substring(0, s.indexOf("\t"));
s = s + subjectName.substring(subjectName.indexOf("\t") + 1, subjectName.length());

This takes the part before the "\t" from s and appends the part after the "\t" from subjectName, thereby effectively removing the "\t". Now if the same string is encountered while iterating subjects for the second mark there is no longer any "\t" in your string and you get an SIOOBE.

It would not happen if you were to write it as:

s = s.substring(0, s.indexOf("\t"));
s = s + subjectName.substring(subjectName.indexOf("\t"), subjectName.length());

But, since you have just previously established that s.substring(0, s.indexOf("\t")) is the same as subjectName.substring(0, subjectName.indexOf("\t")), you could skip this altogether and just write

s = subjectName;
Community
  • 1
  • 1
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Thanks, I found that mistake and fixed it a while ago, but I fixed it like this: s = s + "\t" + subjectName.substring(subjectName.indexOf("\t") + 1, subjectName.length()); I tend to overcomplicate things sometimes :D – Taivna Apr 20 '17 at 16:23