I am making a method which goes into a text file to look into the first index of each line of the file. Then I save each index into an integer array. Once I have the array I make a sentece to get all the values greater than a particular input made by the user. Those values are stored into a new array. When I print the new array into the sentece where I get the values, the action is successful, but when I print the array outside the whole method, the action goes wrong printing some of the values as null. I have been searching for a solution over the internet but nothing helps me out. Don't know what to do... I appreciate in advance your help people.. Here is the code:
public static void getMD_values() throws IOException{
String arch= Read.Leer("Out\\Depth.txt");
String line[] = arch.split("\n");
int totalLines = countLines("Out\\Depth.txt");
int cont1 = 0;
cont = 0;
for(int j=0; j<totalLines; j++){
String md[]=line[j].split(";");
for(int i=0; i<totalLines; i++){
md1[i] = Integer.parseInt(md[0]);
}
if(Entrada.md<md1[j]) {
cont++;
AUX[j]=md1[j];
} else {
}
}
}
public static void testReadAndExtract(String file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
int lineNumber = 0;
getMD_values();
for(int j=0; j<cont; j++){
System.out.println(AUX[j]);
}//for
br.close();
}
This is what I get when printing outside the getMD_values()
null
null
null
1960
1966
null
null
2004
1955
1960
2008
1998
1981
1952
null
1987
1953
null
1954
1958
2001
null
1989
null
null
1982
1973
2005
2002
1951
1978
null
null
null
2007
null
null
null
1994
1952
nullbecauseEntrada.md<md1[j]isfalse? You're not putting anything intoAUX[j]in theelsebranch.j, the index ofAUX, always increases every time in the loop. Imagine a small example, only 2 lines.Entrada.md<md1[j]isfalseonly in the second line. The result:AUX[0]is some number, butAUX[1]isnull. Either you fix the index ofAUXto increase only in thetruecase, or you putif (AUX[j] != null)beforeprintln. :-)cont1andcontvariables ingetMD_values()? Do you need to perhaps assign toAUX[cont]instead ofAUX[j]? You would still getnullatAUX[0], unless you incrementcontafter assigning toAUX[cont]instead of before as you are currently doing.