桂和二进制文件

时间:2017-03-30 14:55:57

标签: java binaryfiles

我目前正在为我所拥有的课程开发一个项目,我们必须创建一个要求输入数字的GUI,然后读取10,000个客户的二进制文件,并返回其他信息。输入的客户编号。当输入1-10,000之外的数字时,我们还必须出现一个错误,该错误会弹出为JOptionPane。我的错误信息有问题,我的教授还说我们需要一个带有此签名的方法:

      private Customer getCustomer(long custNumber) throws IOException

搜索文件并返回客户对象。我也不确定到底要做什么。所以我从我已经知道的东西开始,这就是我所拥有的:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class CustomerLocator extends JFrame
{
private JPanel panel;
private JLabel custNumLabel;
private JLabel custNameLabel;
private JLabel custDisLabel;
private JLabel custBalLabel;
private JLabel custPrefLabel;
private JTextField custNumField;
private JTextField custNameField;
private JTextField custDisField;
private JTextField custBalField;
  private JTextField custPrefField;
  private JButton findCustBut;
  private final int WINDOW_WIDTH = 300;
  private final int WINDOW_HEIGHT = 500;

 public CustomerLocator()
  {
    setTitle("Customer Locator");

    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buildPanel();

    add(panel);

    setVisible(true);
   }

  private void buildPanel()
   {
    custNumLabel = new JLabel("Enter a valid Customer Number: ");

    custNumField = new JTextField(10);

    custNameLabel = new JLabel("Customer Name: ");

    custNameField = new JTextField(10);
    custNameField.setEditable(false);

    custDisLabel = new JLabel("Customer Discount: ");

    custDisField = new JTextField(10);
    custDisField.setEditable(false);

    custBalLabel = new JLabel("Customer Balance: ");

    custBalField = new JTextField(10);
    custBalField.setEditable(false);

    custPrefLabel = new JLabel("Preferred? ");

    custPrefField = new JTextField(10);
    custPrefField.setEditable(false);

    findCustBut = new JButton("Find this Customer!");

    panel = new JPanel();

   findCustBut.addActionListener(new ListenerToFindCustomer());

    panel.add(custNumLabel);
    panel.add(custNumField);
    panel.add(findCustBut);
    panel.add(custNameLabel);
    panel.add(custNameField);
    panel.add(custDisLabel);
    panel.add(custDisField);
    panel.add(custBalLabel);
    panel.add(custBalField);
    panel.add(custPrefLabel);
    panel.add(custPrefField);

}
private class ListenerToFindCustomer implements ActionListener
  {
  public void actionPerformed(ActionEvent e)
  {
        String numEnteredStr;
        long custNumEntered;
        String custName;
        boolean preferred;
        String preferredDisplay;
        double acctBal;
        String acctBalDisplay;
        double discount;
        String discountDisplay;
        numEnteredStr = custNumField.getText();
        custNumEntered = Long.parseLong(numEnteredStr);
        int ct=0;
    try
    {

        FileInputStream inStream = 
        new FileInputStream("c:\\cps\\CustomerObjects.dat");
//          DataInputStream inputFile = new DataInputStream(inStream);
        ObjectInputStream objectInputFile = 
        new ObjectInputStream(inStream);


        while(true)
            {
                ct++;
                Customer obj = (Customer)objectInputFile.readObject();
                //System.out.println(obj.getCustName());
                if (custNumEntered == obj.getCustNum())
                {
                    custName = obj.getCustName();
                    acctBal = obj.getBalance();
                    acctBalDisplay = Double.toString(acctBal);
                    discount = obj.getDiscount();
                    discountDisplay = Double.toString(discount);
                    preferred = obj.getPreferred();
                    preferredDisplay = Boolean.toString(preferred);

                    custNameField.setText(custName);
                    custBalField.setText(acctBalDisplay);
                    custPrefField.setText(preferredDisplay);
                    custDisField.setText(discountDisplay);

                    if (custNameField == null)
                    {
                        JOptionPane.showMessageDialog(null, "Error");
                    }
                }

            }             
    }
    catch (Exception ex)
    {
        System.out.println(ex.toString());

    }
}
}
public static void main(String[] args)
{
new CustomerLocator();
}
}

所以,有了这个,JOptionPane并没有出现,所以我的问题是 1.如何弹出错误消息? 2.我如何结合教授要求的方法?

1 个答案:

答案 0 :(得分:0)

您的代码

                if (custNameField == null)
                {
                    JOptionPane.showMessageDialog(null, "Error");
                }

应该阅读

                if (custName == null)
                {
                    JOptionPane.showMessageDialog(null, "Error");
                }

它也应该移到while循环之外。 一旦到达文件末尾,您还需要一种方法来打破循环。 内部和包括try { ... } catch (Exception ex) { ... }在内的所有内容都可以移动到教师建议的方法中。 您还应该将try-catch更改为另一个catch (IOException ioEx) { throw ioEx; }