2

This is my first time asking on this site, and got my exam tomorrow so trying to make my code great! I will link the whole class (Even though its 3 others).

This is the method, and I cant manage to remove an object from lagerplass, adding it to hk.addVarer works tho. The idea behind all this is that lagerplass is were the items is stored in a store(butikk), so when people buy an item from the store and put it in handlekurv the items gets removed from the storage(lagerplass) :

public void leggTilHandlekurv(String varenavn)
    {
        for (Varer a : lagerplass)
        {
            if (a.getVarenavn().equals(varenavn))
            {
                hk.addVarer(a);
                lagerplass.remove(varenavn);
            }
        }
    }

Here is the kode for the whole Butikk class

public class Butikk
{
// instance variables - replace the example below with your own
public ArrayList<Varer> lagerplass;
Handlekurv hk = new Handlekurv();

/**
 * Constructor for objects of class Butikk
 */
public Butikk()
{
    // initialise instance variables
    lagerplass = new ArrayList<Varer>();
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public void nyHeadset(String lyd, String vare,int pris, String varenavn )
{
    // put your code here
    Headset nyHeadset = new Headset ( lyd, vare, pris, varenavn);
    lagerplass.add(nyHeadset);

}

public void nyMus(String vare, int pris, String varenavn, int dpi)
{
    Mus nyMus = new Mus(vare, pris, varenavn, dpi);
    lagerplass.add(nyMus);
}

public void printLagerplass()
{
    for (Varer vare : lagerplass)
    {
        vare.printDetails();
    }
}

public int lagerplassSize()
{
    return lagerplass.size();
}

public void fjernHeleLagerplass()
{
    lagerplass.clear();
}

public void leggTilHandlekurv(String varenavn) 
{ 
Iterator<Varer> iterator = lagerplass.iterator();
while(iterator.hasNext()) {
     Varer a = iterator.next();
     if (a.getVarenavn().equals(varenavn)) {
         iterator.remove();
         hk.addVarer(a);
     }
}

}

public void printHeleHandlekurven()
{
    hk.printHandlekurv();
}

}enter code here

Here is the kode for the whole Varer class

enter code here public class Varer { // Representerer merke og pris til en vare. private String vare; private String merke; private int pris; private String varenavn;

/**
 * Constructor for klassen Varer
 */
public Varer(int pris, String varenavn, String vare)
{
    merke = "Razor";
    this.pris = pris;
    this.varenavn = varenavn;
    this.vare = vare;

}

public String getMerke()
{
    return merke;
}

public int getPris() 
{
    return pris;
}

public String getVarenavn()
{
    return varenavn;
}

public String getVare()
{
    return vare;
}

public String getDetails()
{
    return vare + ", " + merke +" " + varenavn + ", "+pris + " kr.";
}
public void printDetails()
{
    System.out.println(vare + ", " + merke +" "+ varenavn + ", "+pris + " kr.");
    System.out.println();
}
  • Check out this answer that addresses one aspect of your issue: http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing. You will also need to be concerned about the fact that you are attempting to remove a String from a collection of Varer. – pens-fan-69 May 20 '15 at 14:24
  • For a full answer, please provide the declarations of the two Collections and the Varer class. – pens-fan-69 May 20 '15 at 14:31
  • FYI, removing from an `ArrayList` (unless you remove the last item) never ever looks _great_. (Using any other language than English is not really a great looking thing either.) – Vlasec May 20 '15 at 15:10

3 Answers3

3

Removing an item from a collection while iterating over it should yield a ConcurrentModificationException, which I presume is what you are getting and is why you cannot remove from it.

To remove an element while iterating over a collection, you will need to get that collection's iterator and remove it through the collection's iterator.

public void leggTilHandlekurv(String varenavn) { 
    Iterator<Varer> iterator = lagerplass.iterator();
    while(iterator.hasNext()) {
         Varer a = iterator.next();
         if (a.getVarenavn().equals(varenavn)) {
             iterator.remove();
             hk.addVarer(a);
         }
    }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • I gave this an up vote for the ConcurrentModificationException, but he may also have issues with attempting to remove a String from a collection of Varer, depending on whether/how he overrode .equals() in Varer. – pens-fan-69 May 20 '15 at 14:33
  • @pens-fan-69: According to the Javadoc, the `remove` removes the last element returned by the iterator. It does not seem to be using `equals`. If that is not what you mean, please let me know. – npinti May 20 '15 at 14:35
  • I tried this and it removes, but it wont let me use my print method, then I get an error: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.remove(ArrayList.java:474) at Butikk.leggTilHandlekurv(Butikk.java:69) – Kenneth Rønning May 20 '15 at 14:41
  • @KennethRønning: I do not know what your `print` method does, so I can only speculate, but it would seem that you are using `.get(1)` on some `ArrayList` which is of size 1 (`ArrayLists` are 0 based in Java). – npinti May 20 '15 at 14:43
  • public void printLagerplass() { for (Varer vare : lagerplass) { vare.printDetails(); } } – Kenneth Rønning May 20 '15 at 14:43
  • @KennethRønning: and what does `vare.printDetails()` do? – npinti May 20 '15 at 14:45
  • @npiniti It's supposed to print the details of the objects. public void printDetails() { System.out.println(vare + ", " + merke +" "+ varenavn + ", "+pris + " kr."); System.out.println(); } } – Kenneth Rønning May 20 '15 at 14:46
  • @KennethRønning: It would seem to be OK, are you sure that this is where the error is being thrown? Please follow your stack trace and try to get to the line which is throwing the exception. – npinti May 20 '15 at 14:49
  • Yeah, I get the error when I try to use the method : public void printHandlekurv() { for (Varer a : handlekurv) { System.out.println (a.getDetails()); System.out.println(); } } – Kenneth Rønning May 20 '15 at 15:01
  • @KennethRønning: So that is what is at `Butikk.java, Line: 69`? Sorry for all the questions, but I cannot understand why you are getting that error. – npinti May 20 '15 at 15:03
  • @nipinti Dont worry about all the questions! You're helping me, that's great. The error on line 69 in Butikk class is hk.addVarer(a); – Kenneth Rønning May 20 '15 at 15:08
  • @KennethRønning: This might suggest that the error lies within the `addVarer` method. Can you please post the code for that method or better edit your question to include it? – npinti May 20 '15 at 15:12
  • @npinti You are correct about the remove(). What I meant to say was he may not correctly match depending on his implementation (or lack thereof) of .equals(). – pens-fan-69 May 20 '15 at 15:22
  • @npinti as I look closely, you (and he) are comparing a String field of the Varer. My mistake. Your answer stands on its own as correct. – pens-fan-69 May 20 '15 at 15:26
  • @pens-fan-69: Not a problem. Thanks a lot for looking into it :). – npinti May 20 '15 at 15:27
0

Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

for (Iterator<Varer> iterator = lagerplass.iterator(); iterator.hasNext();) {
    Varer a = iterator.next();
    if (a.getVarenavn().equals(varenavn))
    {
        hk.addVarer(a);
        // Remove the current element from the iterator and the list.
        iterator.remove();
    }
}

Source:

http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

It is because you cannot modify a collection while iterating over it. Either use a for loop with indeces, or an Iterator over your collection and call the remove method.