-2

I am beginner about Java and I need help. I just want to get values from text file, then compare the values with an Object's variable. But somehow, it works in a wrong way. I spent more than 4-5 hours an I got stuck in. Here is my code.

    private List<Course> hs = new ArrayList<>();
    private List<String> list = new ArrayList<>();
    private String[] properties;

public void readLines() {
    try (BufferedReader br = Files.newBufferedReader(Paths.get(Instructor.root.toString()), StandardCharsets.UTF_8)) {
        list = br.lines().collect(Collectors.toList());

        for(int i = 0; i < list.size(); i++) {
            properties = list.get(i).split("%");
            Course course = new Course(properties[0]);
            Undergraduate undergraduate = new Undergraduate(properties[0],properties[1], properties[2], properties[3]);
            if(hs.size() != 0 && isObjectInSet(course, hs)) {
                getIfObjectInSet(course, hs).setUndergraduates(undergraduate);
            }else {
                course.setUndergraduates(undergraduate);
                hs.add(course);
            }}
    }catch (IOException e) {
        System.err.println(e);
    }
    //System.out.println(hs.size());
}

    public boolean isObjectInSet(Course object, List<Course> hs) {
    boolean result = false;


    for (int i = 0; i < hs.size(); i++) {
        String arrayedCourse =  hs.get(i).getCourseID();
        String objectsID = object.getCourseID();
        if (objectsID.equals(arrayedCourse)) {
            result = true;
            break;
        }
    }return result; }

public Course getIfObjectInSet(Course object, List<Course> set) {
    Course result = null;

    for (int i = 0; i < set.size(); i++) {
        if (isObjectInSet(object, set)) {
            result = set.get(i);
            break;
        } }return result;
}}

Here is Course class' variables:

public class Course {
private String courseID;
private ArrayList<Undergraduate> undergraduates = new ArrayList<>();

setUndergraduates in Course Class

    public void setUndergraduates(Undergraduate undergraduates) {
    ArrayList<Undergraduate> students = this.undergraduates;
    students.add(undergraduates);
    this.undergraduates = students;
}

And here is text file structure

BIM101%11111111111%Papa%25

Here is debug. Two strings are equal but it tells they are not

https://i.stack.imgur.com/BOO5d.jpg

Metomania
  • 11
  • 3
  • What exactly is the wrong way in which it works? Also, add code for `getIfObjectInSet()` and `setUndergraduates()`. – xs0 Dec 10 '17 at 18:07
  • Sure, I added them. – Metomania Dec 10 '17 at 18:17
  • To make helping you easier (or even possible) we need [mcve] (a.k.a. [SSCCE](http://sscce.org)). You don't need to post your *whole* code, it is even preferred if you remove parts irrelevant to problem you are facing, but that code example still need to be complete (in a way which will let us copy-paste it to your computers and run it without need to modify/add anything). – Pshemo Dec 10 '17 at 18:26

2 Answers2

1

Please override method equals in Your class Course by adding field what you want to compare. Hint

newOne
  • 679
  • 2
  • 9
  • 27
0

I think the problem is in your getIfObjectInSet() - it uses isObjectInSet(), but that returns true if any of the elements match, not if just the current element matches.

In any case, you'd be better off using a Map here - you can avoid these two functions completely:

private Map<String, Course> courses = new TreeMap<>();

public void readLines(Path file) {
    try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
        br.lines().forEach(line -> {
            String[] parts = line.split("%");
            String courseId = parts[0];

            Undergraduate undergraduate = new Undergraduate(courseId, parts[1], parts[2], parts[3]);

            Course course = courses.get(courseId);
            if (course == null) {
                course = new Course(courseId);
                courses.put(courseId, course); 
            }

            course.setUndergraduates(undergraduate); // rename to addUndergraduate?
        });
    } catch (IOException e) {
        throw new RuntimeException(e); // don't just continue on errors..
    }
}
xs0
  • 2,990
  • 17
  • 25
  • It's more useful but the problem was the whole project. What i mean is that I created another project and entered my codes there. Then my code works properly. Thank you for helping to me. – Metomania Dec 12 '17 at 02:27