0

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
7
  • 1
    Perhaps it's null because Entrada.md<md1[j] is false? You're not putting anything into AUX[j] in the else branch. Commented Dec 10, 2015 at 20:09
  • well.. First of all, I am very confused. I guess that when ´Entrada.md<md1[j]´ is false, then it just ignore it. I already tried to write something into the ´else´ sentence, but I still get the last value as null, and not just that, I don't get the full array as well. The only thing where I am clear is that I want to save those values into the array without getting null values Commented Dec 10, 2015 at 20:31
  • 1
    You can't just ignore it because j, the index of AUX, always increases every time in the loop. Imagine a small example, only 2 lines. Entrada.md<md1[j] is false only in the second line. The result: AUX[0] is some number, but AUX[1] is null. Either you fix the index of AUX to increase only in the true case, or you put if (AUX[j] != null) before println. :-) Commented Dec 10, 2015 at 20:45
  • Thanks for your response bro. I tried your advice, and then I realized that actually the null values are values greater than the Entrada.md, meaning that they got lost or damaged. When I used ´if (AUX[j] != null)´ before println it won't print all the values because it will skip the already missed null values. It is very weird, I am frustrated. I don't get it. Commented Dec 10, 2015 at 20:55
  • 1
    What are the cont1 and cont variables in getMD_values()? Do you need to perhaps assign to AUX[cont] instead of AUX[j]? You would still get null at AUX[0], unless you increment cont after assigning to AUX[cont] instead of before as you are currently doing. Commented Dec 11, 2015 at 2:12

1 Answer 1

1

As mentioned in my comment:

What are the cont1 and cont variables in getMD_values()? Do you need to perhaps assign to AUX[cont] instead of AUX[j]? You would still get null at AUX[0], unless you increment cont after assigning to AUX[cont] instead of before as you are currently doing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.