Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This is my part of java project.Does anyone have any idea about my question that there are problems with delete lesson method in student class.It does not work correctly.Because when i want to delete lesson from listLesson ,the output is "No Exist!",but actually i have created a lesson object and added that lesson to listLesson.Really i dont know what is the problem.May be it is related with addlesson method,i think it does not add lesson to listLesson,so when i wan to delete it is output is" No Exist!".Please,if you have any idea about solving this problem,let me know.Thanks in advance :)
public class student {
    private String name;

    private int id;
    private lesson[]listLesson;
    private int size;
    private int number=0;
    
   
    public student(String name){
        this.name=name;
        listLesson = new lesson[size];
        this.size=5;
    }
    public boolean isExist(lesson l){
        for(int i=0; i < number; i++)
            if(listLesson[i].equals(l))
                if(listLesson[i].getNote()>=60)
                    return true;
        return false;
    }
    public void addLesson(lesson l){     
        if(!this.isExist(l)){
            if(number==size){
               lesson[] newList = new lesson[size+1];
               for(int i=0; i < size; i++){
                   newList[i]=listLesson[i];
               }
               listLesson=newList;
               size++;     
                // System.out.println(listLesson);
            }
            
            listLesson[number]= l;
            number++;
       }
    
        else
            System.out.println("Already passed :"+ l);
            
    }

    public student(String name, int id) {
        this.name = name;
        this.id = id;
    }
   
    public void deleteLesson(lesson l){
        int index=existList(l);
        if(index!=-1){
            lesson []newL=new lesson[size-1];
            for(int i=0;i<index;i++){
                newL[i]=listLesson[i];
            }
            for(int i=index+1;i<size-1;i++){
                newL[i]=listLesson[i+1];
             
            }
               listLesson=newL;
                size--;           
                
        }else{
            System.err.println("No Exist!");
        }
        
    }
    
        public int existList(lesson l){
        for(int i=0;i<number;i++){
            if(listLesson[i].equals(l.getCode())){
                return i;
            }
        }
        return -1;
    }

    public lesson[] getListLesson() {
        return listLesson;
    }





    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setListLesson(lesson l) {
        this.listLesson=new lesson[size];
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public boolean equals(student s) {
        if (this.id == s.id && this.name.equals(s.name)) {
            return true;
        }
        return false;
    }

 


    
   public String toString() {        
 String res="{";
  int i;
  for(i=0;i<size-1;i++){
      if(listLesson.length==0)
          res+="-, ";
      else
      res+=listLesson[i]+", ";
      
  }
  if(listLesson.length==0)
      res+="-";
  else
  res+=listLesson[i];
 
  return "student{" + "name=" + name + ", id=" + id + res+"}";
          }

};


What I have tried:

I have created some classes. But there are some problems.If someone can help me,please tell me.
Posted
Updated 7-Jan-17 20:47pm
v5
Comments
Richard MacCutchan 6-Jan-17 12:46pm    
If you want help then you need to provide some proper details. We cannot guess what your code is doing wrong, or what is meant by "there are some problems."
Member 12702056 6-Jan-17 12:48pm    
Can you give your email,then i will send you my complete project,then you wil able to see the problem.
Richard MacCutchan 6-Jan-17 12:51pm    
No, this is an open forum, and if you want help then you need to explain your problem in proper detail.
Member 12702056 6-Jan-17 12:58pm    
there is a problem with delete lesson method in student class.
Patrice T 6-Jan-17 20:01pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

In your delete method you call existList which does the following:
Java
public int existList(lesson l){
    for(int i=0;i<number;i++){
        if(listLesson[i].equals(l.getCode())){
            return i;
        }
    }
    return -1;
}

Does getCode() return the lesson object? If not then the equals test will most likely fail. You also declare this method to return an int but you try to return a lesson on success.
 
Share this answer
 
Comments
Member 12702056 8-Jan-17 2:56am    
Then how can correct it?If you have opinion,please let me know.Becuase i have tried many ways to solve this problem ,but i can't solve it:/
Richard MacCutchan 8-Jan-17 3:09am    
At a guess you need to compare like with like. What does getCode return, and does it match what you are trying to compare against? And do not return different types from the same method. If you do not understand these basic concepts then go back to your reference guides and do some reading.
Member 12702056 8-Jan-17 3:33am    
Thanks for you advice :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900