I am new to Java, I apologize if I am not using the correct lingo.
A list is loaded from a text file
static List<Media> list=new ArrayList<>();;
public boolean LoadMedia(String path) {
try {
File myObj = new File(path);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] str = data.split("-");
if (str[0].equals("EBook")) {
list.add(new EBook(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));
} else if (str[0].equals("MusicCD")) {
list.add(new MusicCD(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));
} else if(str[0].equals("MovieDVD")) {
list.add(new MovieDVD(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));
}
}
I have a Constructor in a class called media
public Media(String name, int id, String title, int year, String chapter, boolean available) {
this.name = name;
this.id = id;
this.title = title;
this.year = year;
this.chapter = chapter;
this.available = available;
}
This gets output to a string
@Override
public String toString() {
return name + " [id=" + this.id + " title=" + this.title + " chapters=" + this.chapter + " year=" + this.year + " available=" + this.available + "]\n";
}
When a User rents an item:
public void rentMedia(int id) {
for(Media mediaById : list ) {
if(mediaById.getId()==id) {
if(mediaById.isAvailable()) {
System.out.println("media successfully rented out ");
// some code here to update Boolean from true to false
} else {
System.out.println("Media with id="+id+" is not available for rent ");
}
}
}
}
When the output is loaded again I would like for the output to now say false
I have tired list.set however get the following error:
list.set(5, false);
The method set(int, Media) in the type List<Media> is not applicable for the arguments (int, boolean)
As mentioned by Louis Wasserman and Traian GEICU
list.get(5).setAvailable(false);
Required me to create a setAvailable see below
public void rentMedia(int id) {
for(Media mediaById : list ) {
if(mediaById.getId()==id) {
if(mediaById.isAvailable()) {
System.out.println("media successfully rented out ");
System.out.print(mediaById.toString());
list.get(5).setAvailable(false);
} else {
System.out.println("Media with id="+id+" is not available for rent ");
}
}
}
}
public boolean setAvailable(boolean b) {
available = false;
return available;
}
Which threw an error
EBook [id=121 title=Shadow and Bone chapters=20 year=2012 available=true]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 2
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at Manager.rentMedia(Manager.java:82)
at FinalProject.main(FinalProject.java:91)
Setting the index to something below 2 still did not change the true to false, not that it would but what I was hoping to see was one of the indexes change to false or get an error.
list.get(1).setAvailable(false);
Output:
Enter media id :
121
media successfully rented out
EBook [id=121 title=Shadow and Bone chapters=20 year=2012 available=true]
Please see screen shot below.

list.get(5).setAvailable(false);?list.set(5, false);should be invoked with as instance of Media.list.set(5, new Media("name" ... , false). Now think if is more advantageous toreplace and Object with new onejust for update one field, or maybe usegetto retrieve object and just update the field.list.get(id).setAvailable(false);alsosetAvailableshould be placed onMedia.(related to current record) . Mainly even if is not recommended to update fields directly you could check also withlist.get(id).available =false(assume field is public)setAvailablemethod is broken: It completely ignores its argument, always sets available to false regardless of argument, and then for some bizarre reason returnsfalse. You just wantthis.available = available;.