简体   繁体   中英

Cannot update an array in Main from nested loops inside of Main. How can I assign it's value?

So, I am writing my program when suddenly, I run into a problem in which my three arrays print as null, 0 and null.

The program reads values from a file, and assigns them to the three arrays based on the iteration number.

Here is my code:

String  mushroom []= new String [10];
int  weight [] = new int [10];
String  cooking[] = new String [10];

FileReader fr = new FileReader ("7J_Mushrooms.csv");
BufferedReader br = new BufferedReader (fr);
System.out.println("Mushroom\t\tWeight\t\tCooking\n=======================================================");
String line = "";


while ((line = br.readLine()) != null){

    String [] temp = line.split(",");
    for (int i = 0; i < temp.length; i++){  
        if(i == 0){
            mushroom[i] = temp[i];
        }
        else if (i == 1){
            weight[i] = Integer.parseInt(temp[i]);  
        }
        else{
            cooking[i] = temp[i];
        }
    }               
 }

// This bit just sorts them by weight in ascending order like a parallel array
for (int i = 0; i < weight.length-1; i++){
    for (int j = 0; j < weight.length-1;j++){
        if (weight[j] > weight[j+1]){

            int temp = weight [j];
            String tempMush = mushroom [j];
            String tempCook = cooking [j];
            weight[j] = weight[j+1];
            mushroom[j] = mushroom[j+1];
            cooking[j] = cooking[j+1];
            weight[j+1] = temp;
            mushroom[j+1] = tempMush;
            cooking[j+1] = tempCook;
        }
    }
}

for (int i = 0; i < weight.length; i++){
    System.out.print(mushroom[i] + "\t\t" + weight[i] + "\t\t" + cooking[i] + "\n");
}

When I print the values of the arrays inside of the for loop, the values are correct, however outside of the while loop the code is printed as null, null and 0. However, the last three values are printed but I am sure that is something to do with my problem.

Anyway, I believe it is to do with scope.

After some searching, I discovered that java is "Pass by Value" instead of "Pass by Reference". I do not really understand this principal, but to my understanding it affects methods in particular, but all of my code is under a single method -- main. I tried to use return inside the for loop and outside but it does not work either!

The way you are initially reading the values in seems quite off: you are placing the corresponding weight and cooking into different indices than the actual mushroom and you are using the index i in completely the wrong way. It should probably be

int i = 0;
while ((line = br.readLine()) != null){

    String[] temp = line.split(",");
    if(temp.length == 3)
        mushroom[i] = temp[0];
        weight[i] = Integer.parseInt(temp[1]);  
        cooking[i] = temp[2];
    } else {
        // some error handling
    }         
    i++;
 }

"When I print the values of the arrays inside of the for loop, the values are correct" is wrong - if you inspect temp the values are correct but mushroom , weight and cooking are never being filled correctly with your code.

Further note:

  • try using a custom class to hold the associated values instead of dealing with 3 arrays which magically have something to do with each other. Then sort an array of instances of that class

You are writing/over-writing only first three elements in each mushroom, cooking and weight. As

    i < temp.length,   i <= 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM