I have made two boxes within the Driver, one with names and the others with numbers. They randomly choose a name to a number and print it out. It also prints out if the box of names is empty (true or false) but on the 5th name after it reads out the name it should also read out the box is empty. Instead, it causes an error because all of the boxes are empty. What is wrong with my isEmpty method? Also, can I get it to ignore the error and instead get it to print out ‘The box is empty’?
Driver
import java.util.*;
public class DrawTester
{
/** two boxes within drawTester
* box of names (5)
* box of places at a table (5)
*/
public static void main ( String[] args)
{
String code;
Draw<String> myStringBox = new Draw<>();
Draw<Integer> myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
//scanner to draw when user requests
Scanner scan = new Scanner(System.in);
System.out.print("Let's draw some names. Press 'd' to draw. ('q' to stop)");
code = scan.nextLine();
if (!code.equals("q"))
{
if (code.equals("d"))
{
myStringBox = new Draw<>("Happy", "Lucky", "Freddy", "Muddy", "Fuddy");
myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
for (int index = 0; index < 10 ; index++) {
String person = myStringBox.drawItem();
int seat = myIntegerBox.drawItem();
System.out.println(String.format("%s will occupy seat %s.",
person, seat));
boolean isEmpty = myStringBox.isEmpty();
System.out.println("The box is empty: " + isEmpty);
System.out.print("Enter 'd' to make draw. ('q' to stop)");
code = scan.nextLine();
if (code.equals("q")){
System.out.println("you quit");
break;
}
}
}
} System.out.println("You pressed 'q'");
}
}
Draw class
import java.util.*;
@SuppressWarnings("unchecked")
public class Draw<T>
{
boolean isEmpty = true;
Random random = new Random();
List<T> items;
public Draw(List<T> items) {
this.items = items;
}
public Draw(T...items) {
this(new ArrayList<>(Arrays.asList(items)));
}
public T drawItem() {
if (items.size() > 0) {
int index = random.nextInt(items.size());
return items.remove(index);
} else return null;
}
public boolean isEmpty() {
if (items.size() <= 0) {
isEmpty = true;
} return isEmpty = false;
}
public String toString() {
return items.toString();
}
}
>Solution :
Your method is assigning false to isEmpty and thus always returning it as false. Change it to something simpler as follows:
public boolean isEmpty() {
return items.size() == 0;
}
Or as suggested below by @chrylis-cautiouslyoptimistic- (thanks for the hint):
public boolean isEmpty() {
return items.isEmpty();
}
Regarding your second question, you will need to add an if condition to check if the myIntegerBox is also empty before trying to draw an item:
public class DrawTester {
public static void main ( String[] args)
{
String code;
Draw<String> myStringBox = new Draw<>();
Draw<Integer> myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
//scanner to draw when user requests
Scanner scan = new Scanner(System.in);
System.out.print("Let's draw some names. Press 'd' to draw. ('q' to stop)");
code = scan.nextLine();
if (!code.equals("q"))
{
if (code.equals("d"))
{
myStringBox = new Draw<>("Happy", "Lucky", "Freddy", "Muddy", "Fuddy");
myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
for (int index = 0; index < 10 ; index++) {
String person = myStringBox.drawItem();
if (!myIntegerBox.isEmpty()) {
int seat = myIntegerBox.drawItem();
System.out.println(String.format("%s will occupy seat %s.",
person, seat));
boolean isEmpty = myStringBox.isEmpty();
System.out.println("The box is empty: " + isEmpty);
System.out.print("Enter 'd' to make draw. ('q' to stop)");
code = scan.nextLine();
if (code.equals("q")) {
System.out.println("you quit");
break;
}
} else {
System.out.println("The box is empty");
}
}
}
} System.out.println("You pressed 'q'");
}
}
Still, I think there are some things to improve in the code. For example, why a for loop with the condition index < 10? Why 10?