0

Here is my program

        String thisSchool=buffer.readLine();
        String thisLine;

        while((thisLine = buffer.readLine()) != null){
            if(thisLine=="*")
            {
                thisSchool=buffer.readLine();
            }
            else
            {
                School school = new School(thisSchool);
                Student student = new Student(thisLine, school);
                StudentList.add(student);
            }
       }

My text file reads like this:

School1
A
B
C
*
School2
D
E
*
School3
F

The output of my driver class is:

A School1
B School1
C School1
* School1
School2 School1
D School1
E School1
* School1
School3 School1
F School1

This is what I WANT it to look like

A School1
B School1
C School1
D School2
E School2
F School3

Here's what the problem looks to be The "currentSchool" variable is never changing, and I don't know why! The " * " is treated as a student (I want to use it as a delimiter, i.e., the program will ignore it when it is encountered). Instead, the "if line= * " command is being ignored completely, and consequently, the school never changes, and the students are being written incorrectly

Boann
  • 48,794
  • 16
  • 117
  • 146
Raj Raina
  • 211
  • 1
  • 11

1 Answers1

1
if(thisLine=="*")

Don't use "==" tp compare an Object.

Use the equals() method:

if(thisLine.equals("*"))
camickr
  • 321,443
  • 19
  • 166
  • 288
  • wow, I feel really bad now... :) Thanks! – Raj Raina Nov 25 '13 at 04:10
  • == is useful for comparison, but not in this case. == is used if you want to determine whether two primitives are identical, or whether two objects are the same object. In this case, it doesn't work because thisLine and "*" come from different sources, so they will never be == even if they are identical. That's why you use .equals(...) – Vitruvie Nov 25 '13 at 04:11
  • Yeah I know better than to use ==... I was just too focused on another syntactical error to notice my mistake – Raj Raina Nov 25 '13 at 04:19