0

Regarding the following code - I'm trying to get it throw and exception if anything other than a number is entered into 'cost' when adding a book, as you can see I've gotten someway towards doing so but I just want to display a message dialog stating "enter a number please", instead of an input dialog appearing, when i try to change to an message dialog Eclipse returns errors. Any suggestions please?

The relevant snippet is

do{
        try { 
            cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
            book.setCost(cost);
            goodInput = true;
            } 
            catch (NumberFormatException nfe){ 
            cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again"
            )); 
            } 
        }while (!goodInput);

and here's the full code. thanks.

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;
    double total = 0;
    boolean goodInput = false;


    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("Hightest Price Paid");
        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");
        //cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
        do{
        try { 
            cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
            book.setCost(cost);
            goodInput = true;
            } 
            catch (NumberFormatException nfe){ 
            cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again"
            )); 
            } 
        }while (!goodInput);


        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 + "and it costs" + cost + "euro";
        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 " + total + costOfBookshelf + "Euro";
            JOptionPane.showMessageDialog(this, message);
        }

    }
    }
1
  • what are the errors that you are getting? Commented Mar 2, 2011 at 22:28

1 Answer 1

2

You shouldn't try to read what the user enters in the message dialog, when an exception occurs : a message dialog is not meant to input anything. That's why showMessageDialog returns void, and not String like showInputDialog. Replace

catch (NumberFormatException nfe){ 
        cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again"
        )); 

with

catch (NumberFormatException nfe){ 
        JOptionPane.showMessageDialog(this, "Numerical entry expected. Please try again"); 

Also, note that you should always pass a parent component in the showXXXDialog, in order for the dialog to be modal (i.e. to block the access) to the frame of this component. Have you read the javadoc of JOptionPane? Have you read the tutorial about dialogs?

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

2 Comments

Thank you. no i haven't read that tutorial but i will now, thanks. one final question pls, what's the string equivalent of numberformat exception so that an error is thrown if numbers are entered instead of characters?
There is none. You'll have to analyze the string and determine if it's valid or not. You could do that with a regular expression. Read download.oracle.com/javase/tutorial/essential/regex

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.