0

Line 100 cost = JOptionPane.showInputDialog("Cost"); of the attached code is reporting the error "Type mismatch: cannot convert from String to double" - any suggestions what's causing this please?

Basically it's a class that accesses another class 'BookShelf' and this line is part of the actioncommand that accesses the BookShelf class to return costofBookshelf

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener; 
import java.util.ArrayList;

public class BookGUI extends JFrame implements ActionListener

{

    //String addBook="";
    // public ArrayList<Book> books;

    Book books = new Book ("", "", 0, "", 0);
    Book book = new Book("", "", 0, "", 0);
    String title  = "";
    String author  = "";
    int year = 0;
    String publisher  = "";
    double cost = 0;

    public BookShelf bookShelf = new BookShelf();
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    //Creates & displays a window of the class FlowLayoutDemo
    public static void main(String[] args)
    {
        BookGUI gui = new BookGUI( );
        gui.setVisible(true);
    }

   // public String getTitle()
   // {
    //    return title;
    //}

    public void setTitle(String title) //this is relevant
    {
        this.title = title;
    }

    public void setAuthor(String author) //this is relevant
    {
        this.author = author;
    }

    public void setYear(int year) //this is relevant
    {
        this.year = year;
    }
    public void setPublisher(String publisher) //this is relevant
    {
        this.publisher = publisher;
    }

    public void setCost(double cost) //this is relevant
    {
        this.cost = cost;
    }

    public BookGUI( )
    {

        setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());

        JButton button1 = new JButton("Title");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);

        JButton button2 = new JButton("Cost of Bookshelf");
        content.add(button2);
        button2.addActionListener(this);

        JButton button3 = new JButton("Size of BookShelf");
        content.add(button3);
        button3.addActionListener(this);

        JButton button4 = new JButton("Add Book");
        content.add(button4);
        button4.addActionListener(this);     

    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Add Book"))
       //book = JOptionPane.showInputDialog("Add Book");
        {     //set up the book object with all the data passed in
        title = JOptionPane.showInputDialog("Title");
        author = JOptionPane.showInputDialog("Author");
        publisher = JOptionPane.showInputDialog("Publisher");
        ***cost = JOptionPane.showInputDialog("Cost");***
        book.setTitle(title);
        book.setAuthor(author);
        book.setPublisher(publisher);
        bookShelf.addBook(book);

        String message =  "The title of the book is :" + title + 
        "the Author of the Book is : " + author + " and it's published by " + publisher;
        JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);
        }
        else if (e.getActionCommand().equals("Size of BookShelf")) {
            int sizeOfBookShelf = bookShelf.sizeOfBookshelf();
            String message = "The book shelf has " + sizeOfBookShelf + " book(s)";
            JOptionPane.showMessageDialog(this, message);
        }
        else if (e.getActionCommand().equals("Cost of BookShelf")) 
        {
            double costOfBookshelf = bookShelf.costOfBookshelf();
            String message = "The book shelf value is " + costOfBookshelf + "Euro";
            JOptionPane.showMessageDialog(this, message);
        }

    }
}

4 Answers 4

1

JOptionPane.showInputDialog returns a String. You can't assign a String value to a variable of type double.

You can use Double.parseDouble to convert the String into a double.

1

cost is a double and showInputDialog returns a string. So you need to do this:

cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));

However, if this is intended for a production code, you should probably add some validation to make sure the value entered is actually numeric.

1
  • thanks, it's an assignment i'm finishing off - my final job is to do some validation but thanks for the advice. can you tell me what the code that you've suggested is actually doing to make this work please - what does Double.parseDouble do?
    – Simon
    Mar 1, 2011 at 16:44
1
cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));

Using double to represent money may give you bad results though, you should consider using BigDecimal instead.

2
  • thanks for the quick responses, i'll add this and report back shortly, S
    – Simon
    Mar 1, 2011 at 16:41
  • You may consider using BigDecimal instead, using double may cause problems if you need an exact result. See the update
    – OscarRyz
    Mar 1, 2011 at 16:42
0

You have to parse the string input into a double.

try {
   code = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
}
catch (NumberFormatException ex){
   code = 0.0;
}
2
  • Most of the time this kind of error handling is a bad idea. Better tell user about error or prevent typing incorrect value. Mar 1, 2011 at 16:44
  • you are probably better off handling the NumberFormatException through some kind of error reporting mechanism rather than simply setting 0.0 . Otherwise there is user difference between explicitly setting 0.0, or getting there through a String value unparsable as a double Mar 1, 2011 at 16:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.