简体   繁体   中英

set in array from one method and get from another method

I have made an array from class student

student [] stu=new student[100];

I'm in class student op and I want to insert name and code from method insert to array stu .

Until now everything worked, but when I want to get my name and code from another method like delete in class student op , it shows that my array stu is empty. Where is the problem? if I delet the line
stu[0]=new student(); in delet method Net beans show an error!!

public class studentop {

     student [] stu=new student[100];

    private int counter=0;
    int con=0;
    public int edd;

    public void insert(int edame){

        String ans;
        Scanner input=new Scanner(System.in);
        for(int i=edame;i<100;i++){
            System.out.println("add more student? ");
            ans=input.next();
            if(ans.equals("y")){

                String thename;

                stu[i]=new student();
                System.out.println("insert name  "+(i+1)+" : ");

                thename = input.next();
                stu[i].setname(thename);

                System.out.println("insert code  "+(i+1)+" : ");
                int thecode;
                thecode = input.nextInt();
                stu[i].setcode(thecode);

                System.out.println(stu[i].getcode());

                for(int m=0;m<=3;m++){
                    System.out.println("add mark  "+(m+1)+" ? ");
                    String ans2;
                    ans2=input.next();
                    if(ans2.equals("y")){
                        switch (m){

                            case 0:
                                System.out.println("mark"+(m+1)+" : ");
                                int mark;
                                mark=input.nextInt();
                                stu[i].setmark(mark);
                                break;

                            case 1:
                                System.out.println("mark"+(m+1)+" : ");
                                //  int mark;
                                mark=input.nextInt();
                                stu[i].setMark2(mark);
                                break;

                            case 2:
                                System.out.println("mark"+(m+1)+" : ");
                                //  int mark;
                                mark=input.nextInt();
                                stu[i].setMark3(mark);
                                break;

                            case 3:
                                System.out.println("mark"+(m+1)+" : ");
                                // int mark;
                                mark=input.nextInt();
                                stu[i].setMark4(mark);
                                break;
                        }
                    }
                    else{
                        break;
                    }
                }
                System.out.println(stu[i].getname());//aztarighe get name az 
            }
            else {
                edame=i;
                edd=edame;
                break;
            }
        }//end for
    }
    public void delete(){
        stu[0]=new student();//if I delet this line it shows an error!
        System.out.println(stu[0].getcode());
    }
}

and here is my class student

public class student {
    public String name;
    public int code;
    public double mark;
    private double mark2;
    private double mark3;
    private double mark4;

    public void setname(String sourcename){
        name=sourcename;
    }
    public String getname(){
        return name;
    }

It is not empty, however, when you try to get the student from the array you're creating a new student that's why it appears to be empty:

public void delete()
{
    stu[0]=new student(); //here you're deleting your old student
    System.out.println(stu[0].getcode());
}

Instead you could use:

public void delete()
{
    System.out.println(stu[0].getcode());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM