Java GUI错误-类型不匹配:无法从字符串转换为双精度

Java GUI错误-类型不匹配:无法从字符串转换为双精度,java,swing,Java,Swing,第100行cost=JOptionPane.showInputDialog(“cost”)的附加代码报告错误“类型不匹配:无法从字符串转换为双精度”-有什么建议导致此问题吗 基本上,它是一个访问另一个类“BookShelf”的类,此行是访问BookShelf类以返回BookShelf成本的actioncommand的一部分 import java.awt.FlowLayout; import java.awt.Container; import java.awt.event.ActionEven

第100行
cost=JOptionPane.showInputDialog(“cost”)的附加代码报告错误“类型不匹配:无法从字符串转换为双精度”-有什么建议导致此问题吗

基本上,它是一个访问另一个类“BookShelf”的类,此行是访问BookShelf类以返回BookShelf成本的actioncommand的一部分

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

    }
}
导入java.awt.FlowLayout;
导入java.awt.Container;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
公共类BookGUI扩展JFrame实现ActionListener
{
//字符串addBook=“”;
//公共图书馆图书;
图书=新书(“,”,0,”,0);
图书=新书(“,”,0,”,0);
字符串标题=”;
字符串author=“”;
整年=0;
字符串publisher=“”;
双倍成本=0;
公共书架=新书架();
公共静态最终整数宽度=300;
公共静态最终内部高度=200;
//创建并显示FlowLayoutDemo类的窗口
公共静态void main(字符串[]args)
{
BookGUI=newbookgui();
setVisible(true);
}
//公共字符串getTitle()
// {
//返回标题;
//}
public void setTitle(字符串标题)//这是相关的
{
this.title=标题;
}
public void setAuthor(字符串作者)//这是相关的
{
this.author=作者;
}
public void setYear(int year)//这是相关的
{
今年=年;
}
public void setPublisher(String publisher)//这是相关的
{
this.publisher=publisher;
}
public void setCost(双重成本)//这是相关的
{
成本=成本;
}
公共BookGUI()
{
设置尺寸(宽度、高度);
addWindowListener(新的WindowDestroyer());
setTitle(“GUI分配”);
容器内容=getContentPane();
content.setLayout(新的FlowLayout());
JButton button1=新JButton(“标题”);
内容。添加(按钮1);
按钮1.addActionListener(此按钮);
//contentPane.add(按钮1);
JButton button2=新JButton(“书架成本”);
内容。添加(按钮2);
按钮2.addActionListener(此);
JButton button3=新JButton(“书架尺寸”);
内容。添加(按钮3);
按钮3.addActionListener(此);
JButton button4=新JButton(“添加书本”);
内容。添加(按钮4);
按钮4.addActionListener(此);
}
已执行的公共无效操作(操作事件e)
{
如果(例如getActionCommand().equals(“添加书本”))
//book=JOptionPane.showInputDialog(“添加书本”);
{//使用传入的所有数据设置book对象
title=JOptionPane.showInputDialog(“title”);
author=JOptionPane.showInputDialog(“author”);
publisher=JOptionPane.showInputDialog(“publisher”);
***cost=JOptionPane.showInputDialog(“cost”)***
书名(书名);
书的作者(作者);
图书出版商;
书架。增补本(书);
String message=“书的标题是:“+title+
该书作者为:“+作者+”,由“+出版商”出版;
showMessageDialog(null,消息,“书籍详细信息”,JOptionPane.PLAIN\u消息);
}
else if(e.getActionCommand().equals(“书架大小”)){
int-sizeOfBookShelf=bookShelf.sizeOfBookShelf();
String message=“书架上有”+sizeOfBookShelf+“本书”;
showMessageDialog(这个,消息);
}
否则,如果(例如getActionCommand().equals(“书架成本”))
{
double costOfBookshelf=bookShelf.costOfBookshelf();
String message=“书架价值为”+书架成本+“欧元”;
showMessageDialog(这个,消息);
}
}
}

JOptionPane.showInputDialog返回一个字符串。不能将字符串值赋给double类型的变量


可以使用Double.parseDouble将字符串转换为Double。

JOptionPane.showInputDialog返回字符串。不能将字符串值赋给double类型的变量

try {
   code = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
}
catch (NumberFormatException ex){
   code = 0.0;
}

您可以使用Double.parseDouble将字符串转换为Double。

cost
是一个Double,而
showInputDialog
返回一个字符串。因此,您需要这样做:

try {
   code = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
}
catch (NumberFormatException ex){
   code = 0.0;
}
cost=Double.parseDouble(JOptionPane.showInputDialog(“cost”))


但是,如果这是针对生产代码的,您可能应该添加一些验证,以确保输入的值实际上是数字。

cost
是双精度的,并且
showInputDialog
返回一个字符串。因此,您需要这样做:

cost=Double.parseDouble(JOptionPane.showInputDialog(“cost”))


但是,如果这是针对生产代码的,您可能应该添加一些验证,以确保输入的值实际上是数字。

您必须将字符串输入解析为双精度

try {
   code = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
}
catch (NumberFormatException ex){
   code = 0.0;
}

您必须将字符串输入解析为双精度

try {
   code = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
}
catch (NumberFormatException ex){
   code = 0.0;
}
使用时,你应该考虑。
使用时,你应该考虑。<谢谢你的快速回答,我会加上这个,并很快报告,SYou可能会考虑使用BigDigMall代替,如果你需要一个精确的结果,使用双可能会导致问题。请参阅UpDeTeNANKS的快速响应,我会添加这个,并报告不久,SYou可能会考虑使用BigDigMall代替,使用双可能会导致问题,如果你需要一个确切的结果。请看updateMost,这种错误处理是一个坏主意。最好告诉用户错误或防止键入错误的值。您可能最好通过某种错误报告机制来处理NumberFormatException