0

I'm making an ArrayList that takes Strings, and one method in the class takes a String, adds "urgent:" to the front of it, and puts it at the beginning of the list. I'm now writing a method that is supposed to take an int, a String's position, and 'demote' that string to the end of the list.

However, if the string has "urgent:" in front of it, the method is also supposed to remove "urgent:" if it's demoted. Everything compiles and runs, but it's not removing "urgent:" and I'm not sure why.

public void addUrgetItem(String newItem) {
    String urgentItem = newItem; 
    items.add(0, "urgent: " + urgentItem); 
}

public void demote(int position) {
    int size = items.size();  
    String itemAtSpot = items.get(position); 
    items.remove(itemAtSpot); 
    items.add((size-1), itemAtSpot); 

    if (itemAtSpot.startsWith("urgent:")) {
        items.remove("urgent:"); 
    }
}

2 Answers 2

5

When you say this:

items.remove("urgent:");

It's trying to remove the literal String "urgent" from the list itself, not from any specific item in the list. Instead, you need to remove that text from itemAtSpot before you add it back into the List. That would look something like this:

public void demote(int position) {
    int size = items.size();  
    String itemAtSpot = items.get(position); 
    items.remove(itemAtSpot); 
    if (itemAtSpot.startsWith("urgent: ")) {
        itemAtSpot = itemAtSpot.substring("urgent: ".length());
    }
    items.add(itemAtSpot); 
}

So after you remove the String, you take the substring of everything after "urgent:", and add that back onto the List at the end.

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

Comments

4

By calling items.remove("urgent:"); you're trying to remove a String that doesn't exist from the ArrayList, a String that contains "urgent" and nothing but urgent. Instead you must remove "urgent" from the String that is held by the ArrayList. This can be done by using String's replace(...) method, replacing "urgent:" with "": myString.replace("urgent:", "");

But having said this you actually could be doing things a whole lot better since you're doing a common programming evil: using Strings to hold logic information, more logic than they should. Instead of having the ArrayList hold Strings, why not have it hold objects of a custom class, a class that can have a boolean urgent field, fields that can easily be set via methods?

e.g.,

public class MyItem {
    private String text;
    private boolean urgent = false;

    public MyItem(String text) {
        this.text = text;
    }

    public MyItem(String text, boolean urgent) {
        this.text = text;
        this.urgent = urgent;
    }

    public boolean isUrgent() {
        return urgent;
    }

    public void setUrgent(boolean urgent) {
        this.urgent = urgent;
    }

    public String getText() {
        return text;
    }

    @Override
    public String toString() {
        return text;
    }

    // ... equals and hashCode methods based on text only
}

and....

private List<MyItem> items = new ArrayList<>();

// .....

public void addUrgetItem(String newItem) {
    MyItem item = new MyItem(newItem, true); 
    items.add(0, item); 
}

public void demote(int position) {
    int size = items.size();  
    MyItem itemAtSpot = items.get(position); 
    items.remove(itemAtSpot); 
    itemAtSpot.setUrgent(false);
    items.add((size-1), itemAtSpot); 
}

Comments

Your Answer

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