java - Array filling that users inputs with Method -


i need project. i'm writing program show students informations users fills previously. how did learn methods. wrote program out methods. when write same things method encountered pass reference... filling 0. index when fill 1. index, 0. index became null. try cannot solve problem think thats returns... here code can me way coz can see java language level beginner:) ;

//=========================================================== methods    public static void record(string x, int y)    string[] stringarray = new string[100];    stringarray[y] = x;    return stringarray;    } public static double[] record(double x, int y){ double[] doublearray = new double[100]; doublearray[y] = x; return doublearray; }  

and option ;

 case 1:  {     system.out.println("** recording new student");     system.out.println("*** please use lower case");     in.nextline(); // solve skipping       system.out.print("enter student name , surname: ");     string namex = in.nextline();     name=record(namex,accountnumber);       system.out.print("enter student gender(m/f): ");     string genderx = in.nextline();     gender=record(genderx,accountnumber);      system.out.print("enter student number: ");     string studentnox = in.nextline();     studentno=record(studentnox,accountnumber);       system.out.print("enter student gpa: "); // dont use method here testing     gpa[accountnumber] = in.nextdouble();       accountnumber++;       system.out.println("new student recorded. there ["+accountnumber+"] students in system.");     system.out.println("");   }break; 

the problem you're initializing array every time put inside it:

string[] stringarray = new string[100];  double[] doublearray = new double[100]; 

you must make sure initialization of these arrays happens once in application, when declaring these static arrays. knowing that, record methods should (based on code):

public static void record(string x, int y)     stringarray[y] = x; }  public static void record(double x, int y) {     doublearray[y] = x; } 

also, basic knowledge, java never passes reference, pass value. more info here: is java "pass-by-reference" or "pass-by-value"?


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -