jkjk
jkjk

Reputation: 101

JAVA "cannot be cast to java.lang.String"

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);
        }
    }

Upvotes: 0

Views: 18965

Answers (3)

Ali Seyedi
Ali Seyedi

Reputation: 1817

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.

Upvotes: 2

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

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();

Upvotes: 1

Ibukun Muyide
Ibukun Muyide

Reputation: 1298

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;
}

Upvotes: 1

Related Questions