0

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);
        }
    }
3
  • 3
    Please take the time to indent your code appropriately and remove excessive vertical whitespace. The code is much harder to read than it needs to be at the moment. I'd also suggest reducing it to a minimal reproducible example. Fundamentally, it sounds like you're trying to cast a reference to an object of type Book to String, and that's just not going to work. Commented Apr 5, 2016 at 21:28
  • 3
    java.lang.ClassCastException: Books cannot be cast to java.lang.String I assume that this exception occurs in another part of your code... a stacktrace would help. Commented Apr 5, 2016 at 21:30
  • if (userChoice.equals("Option1")) {} else if (userChoice.equals("Option2")) { Just drop the first condition, it is redundant: if (userChoice.equals("Option2")) {. Commented Apr 5, 2016 at 21:33

3 Answers 3

2

There is a toString() method available in java which can be called on any object. But if you want to get an appropriate behavior of this method based on the characteristics of your Book, you should override it like in the Ibukun's answer.

Casting concept is used to get a more specific type of an Object. So since the book is not an instanceof the String you get a ClassCastException at run time.

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

Comments

1

Here, I reproduced the same behavior

public static void main(String[] args) {
    Book[] ani = new Book[]{new Book(), new Book()};
    String usi = (String) JOptionPane.showInputDialog(null, "Books", "Books", JOptionPane.QUESTION_MESSAGE, null, ani, null);
}

Do not forget one thing, this Overload of showInputDialog returns the choosen Object (here a Book) from the array passed in the parameters.

You certainly want to assign toString() or getName() from the returned Object


Solution

Book tmp = (Book) JOptionPane.showInputDialog(null, "Books", "Books", JOptionPane.QUESTION_MESSAGE, null, ani, null);
String userChoice = tmp.getName();
String userChoice2 = tmp.toString();

Comments

1

Not sure exactly what the problem is, you didn't post your Book Class. In General, every class has a toString() function inbuilt, but this may not necessarily be suited for your class. I suggest to override the toString() function in your Book class. Something like this.

@override
public String toString(){
  // assign result to your intended string
  return result;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.