2011-03-01 71 views
0

线100 cost = JOptionPane.showInputDialog("Cost");附加代码报告错误“类型不匹配:不能从字符串转换为双” - 任何建议是什么导致这个吗?Java的GUI错误 - 类型不匹配:不能从字符串转换为加倍

基本上它的访问另一个类“书架”一类,这行是访问书架类返回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); 
     } 

    } 
} 

回答

1

JOptionPane.showInputDialog返回一个字符串的actioncommand的一部分。您不能将字符串值分配给类型为double的变量。

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

1

cost是一个双精度和showInputDialog返回一个字符串。所以,你需要这样做:

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

但是,如果这是用于生产的代码,很可能需要添加一些验证,以确保输入的值是实际数字。

+0

在“;”之前缺少“)” – qbert220 2011-03-01 16:38:25

+0

谢谢,这是一个任务我完成了 - 我的最后一项工作是做一些验证,但感谢您的建议。你能告诉我你所建议的代码实际上是在做什么来完成这项工作 - Double.parseDouble是做什么的? – Simon 2011-03-01 16:44:55

0

你必须解析字符串输入到双。

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

大多数情况下,这种错误处理是一个坏主意。最好告诉用户有关错误或防止输入不正确的值。 – 2011-03-01 16:44:17

+0

您可能更适合通过某种错误报告机制来处理NumberFormatException,而不是简单地设置0.0。否则,在显式设置0.0之间存在用户差异,或者通过字符串值达到此位置,因为快速响应而无法解析为双倍 – magicduncan 2011-03-01 16:45:22

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

使用double to represent money may give you bad results虽然,你应该考虑using BigDecimal instead

+0

,我将添加并立即生成报告,S – Simon 2011-03-01 16:41:00

+0

您可以考虑使用BigDecimal,而不是使用double如果您需要确切的结果会导致问题。请参阅更新 – OscarRyz 2011-03-01 16:42:07

+0

+1以提及BigDecimal与double。 – 2011-03-01 16:45:23

相关问题