Reputation: 145
The goal is to modify elements within my objects, so my thought process is to first get the index of the object and then use said index to point me to the correct object to be modified.
I'm searching for the object using the name element within the object, but whenever I print the index to make sure it's collecting the correct one, I keep getting -1 for all names tried, even though objects exists with the name I'm searching for.
public class Main {
public static void main(String[] args) throws IOException, JAXBException {
File file = new File("items.xml");
Scanner scan = new Scanner(System.in);
JAXBContext context = JAXBContext.newInstance(Items.class);
Unmarshaller un = context.createUnmarshaller();
Items itemData = (Items) un.unmarshal(file);
List<Item> items = itemData.getItem();
for(Item e: items){
System.out.println("\nDataType : "+e.getDataType());
System.out.println("Name : "+e.getName());
System.out.println("Data : "+e.getData());
System.out.println("Group : "+e.getGroup());
System.out.println("Sub Categories : " + e.getItems());
System.out.println("--------------------------");
}
System.out.println("'\n\n------------- Options ---------------");
System.out.println("\n1. Search Items by Group");
System.out.println("\n2. Modify Movie");
int choice = scan.nextInt();
scan.nextLine();
if (choice == 2){
System.out.println("\nEnter the name of the movie you would like to modify: ");
String search = scan.nextLine();
System.out.println("Index of: " + items.indexOf(search));
}
Item Class
import javax.xml.bind.annotation.XmlElement;
public class Item {
private String dataType;
private String name;
private int data;
private String group;
private Items items;
@XmlElement
public String getDataType(){
return dataType;
}
public void setDataType(String dataType){
this.dataType = dataType;
}
@XmlElement
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@XmlElement
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
@XmlElement
public String getGroup(){
return group;
}
public void setGroup(String group){
this.group = group;
}
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
@Override
public String toString() {
return
"\n\tDataType: " + dataType +
"\n\tName: " + name +
"\n\tData: " + data +
"\n\tGroup: " + group +
"\n\tItems: " + items + "\n";
}
}
Items class
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "items")
public class Items {
private List<Item> item;
@XmlElement(name = "item")
public List<Item> getItem(){
return item;
}
public void setItem(List<Item> itemList){
this.item = itemList;
}
@Override
public String toString() {
return "\n" + item ;
}
}
Upvotes: 1
Views: 96
Reputation: 18002
You're calling indexOf
of your List<Item> items
with a String
while it expects an Item
. See the official Java Doc
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
To do what you want you could loop over the collection until you find the item that satisfies your search condition and then return the index. Something like this:
int findIndexByItemName(String itemName) {
for(int i = 0; i < items.size(); i++)
if(items.get(i).getName().equals(itemName))
return i;
return -1;
}
Upvotes: 3