Sunday 15 March 2015

java - set in array from one method and get from another method -



java - set in array from one method and get from another method -

i have made array class student

student [] stu=new student[100];

i'm in class student op , want insert name , code method insert array stu.

until worked, when want name , code method delete in class student op, shows array stu empty. problem? if delet line stu[0]=new student(); in delet method net beans show error!!

public class studentop { pupil [] 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 name az } else { edame=i; edd=edame; break; } }//end } public void delete(){ stu[0]=new student();//if delet line shows error! system.out.println(stu[0].getcode()); } }

and here class pupil

public class pupil { 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(){ homecoming name; }

it not empty, however, when seek pupil array you're creating new pupil that's why appears empty:

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

instead use:

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

java arrays

No comments:

Post a Comment