0

i need your help for my project. I'm writing an program which show students informations that users fills previously. this is about how much did i learn the methods. I already wrote a program with out methods. But when i write same things with method i encountered with pass by reference... i am filling the 0. index but when i fill the 1. index, 0. index became a null. I try everything but i cannot solve this problem i think thats about my returns... here is the code can you help me with basically way coz as you can see my Java language level is 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 my option ;

 case 1:  {
    System.out.println("** Recording a new student");
    System.out.println("*** Please use lower case");
    in.nextLine(); // for solve skipping 

    System.out.print("Enter Student Name and 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: "); // i dont use method here for testing
    gpa[accountNumber] = in.nextDouble();


    accountNumber++;


    System.out.println("New Student Recorded. There are ["+accountNumber+"] students in system.");
    System.out.println("");
  }break;
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Gökhan Nas
  • 45
  • 2
  • 8

1 Answers1

1

The problem is that you're initializing the array every time you put something inside it:

String[] stringArray = new String[100];

double[] doubleArray = new double[100];

You must make sure that initialization of these arrays happens just once in your application, probably when declaring these static arrays. Knowing that, your record methods should look like (based on your code):

public static void record(String x, int y)
    stringArray[y] = x;
}

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

Also, as basic knowledge, Java never passes by reference, it only pass by value. More info here: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • You're right , it is pass-by-value. my fault... so, i change my code with these and i have stringArray named array in my main but my compile giving an error for stringArray "...cannot be resolved to a variable" am i need more definiton for these arrays? – Gökhan Nas May 08 '13 at 17:18
  • @GökhanNas From your actual code in your question, I can't provide a solution to your new problem. – Luiggi Mendoza May 08 '13 at 17:19