Timo N.
Timo N.

Reputation: 361

for loop with ArrayList in java

i got a Productclass stored in ArrayList myValues. I want to go trough each xth ( for example each 3th) element and have the user add some Integer values to that class.

In my for(...) he tells me: The left-hand side of an assignment must be a variable. I wonder if i messed up some of the .get(j) or if i have to synchronize my methods so that the size wont change ( there is no multi-threading but maybe thats why i get an error? ) or the solution is more simple.

Thx

public void prioPerProduct (){

    System.out.println("");
    System.out.println("Please enter storing and upgrading cost:");

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    int storingCost = 0;
    int ruestCost = 0;
    int countRes = countRessources;
    int sizemyValues = myValues.size();
    for(int j = 0; j < sizemyValues; j = j+countRes){

        System.out.println("Please enter storingcost " + myValues.get(j).getProduct() +":" );
        try {
            storingCost = Integer.valueOf(br.readLine());
        } catch (NumberFormatException e) {
            System.out.println("No number entered");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("No number entered");
            e.printStackTrace();
        }

        System.out.println("Please enter upgradingcost " + myValues.get(j).getProduct() +":" );
        try {
            ruestCost = Integer.valueOf(br.readLine());
        } catch (NumberFormatException e) {
            System.out.println("No number entered");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("No number entered");
            e.printStackTrace();
        }

        myValues.get(j).setstoringCost(storingCost);
        myValues.get(j).setupgradingCost(ruestCost);


    }


}

Upvotes: 0

Views: 86

Answers (2)

Alex Minjun Yu
Alex Minjun Yu

Reputation: 3707

The left-hand side of an assignment must be a variable is generally caused by something like this:

methodCall() = somvalue...

The left hand side must be a variable rather than a method call. Your code looks fine in this regard.
The code where you declare your model class and a full stack trace are more helpful.

Upvotes: 1

Sage Smith
Sage Smith

Reputation: 441

Use the set method to change the value of an object inside of an ArrayList.

myValues.set(j,myValues.get().setstoringCost(storingCost));
myValues.set(j,myValues.get().setupgradingCost(ruestCost));

Upvotes: 1

Related Questions