I am trying to create a list and output my array of books to it and have the user be able to click one of the books to rent it. The list works and it outputs all of the books however when I click a book and select OK it gives me the error "Exception in thread "main" java.lang.ClassCastException: Books cannot be cast to java.lang.String". Could someone explain to me what this error is and how I can fix it?
public static void CustomerBooks() throws IOException {
Object[] Menu = {
"Option1",
"Option2",
"Option3",
"Option4"
};
String userChoice = (String) JOptionPane.showInputDialog(null, "Welcome", "Book Menu", JOptionPane.QUESTION_MESSAGE, null, Menu, null);
if (userChoice.equals("Option1")) {} else if (userChoice.equals("Option2")) {
File myFile = new File("books.txt");
Scanner inputFile = new Scanner(myFile);
Books[] bookArray = new Books[200];
String str;
while (inputFile.hasNext()) {
str = inputFile.nextLine();
String[] myData = str.split("#");
Books.addBook(bookArray, Books.getBookCount(), myData[0], myData[1], myData[2]);
}
inputFile.close();
String userChoice1 = (String) JOptionPane.showInputDialog(null, "Books List", "Books", JOptionPane.QUESTION_MESSAGE, null, bookArray, null);
JOptionPane.showMessageDialog(null, "You chose: " + userChoice1); {
System.exit(0);
}
}
BooktoString, and that's just not going to work.java.lang.ClassCastException: Books cannot be cast to java.lang.StringI assume that this exception occurs in another part of your code... a stacktrace would help.if (userChoice.equals("Option1")) {} else if (userChoice.equals("Option2")) {Just drop the first condition, it is redundant:if (userChoice.equals("Option2")) {.