簡體   English   中英

NumberFormatException解析為兩倍

[英]NumberFormatException parsing to double

當我從客戶端向服務器發送值時,出現如下異常。 服務器中的語句dString = new Date().toString(); 正在執行而不是處理數據報包。 能否請您提供一些輸入信息以更正服務器或我的客戶端,以獲得准確的輸出。

Exception in thread "Thread-2" java.lang.NumberFormatException: For input string: "45.0Feb 11 03:35:27 CST 2016"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at com.loan.emi.QuoteServerThread.run(QuoteServerThread.java:75)
    at java.lang.Thread.run(Unknown Source)

.//服務器類

public class QuoteServerThread extends JFrame implements Runnable {

    protected DatagramSocket socket = null;
    protected BufferedReader in = null;
    protected boolean moreQuotes = true;
    private JTextArea jta = new JTextArea();

    public QuoteServerThread() throws IOException {
        this("QuoteServerThread");
    }

    public QuoteServerThread(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(8300);
        add(new JScrollPane(jta), BorderLayout.CENTER);
        setVisible(true);
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Server");// It is necessary to show the frame here
        try {
            in = new BufferedReader(new FileReader("one-liners.txt"));
        } catch (FileNotFoundException e) {
            jta.append("Couldn't open quote file. Serving time instead.");
        }
    }

    public void run() {
        while (moreQuotes) {
            try {
                byte[] buf = new byte[256];
                // Receive request for processing
                DatagramPacket packet
                        = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                // Figure out response
                String dString = null;
                if (in == null) {
                    dString = new Date().toString();
                } else {
                    dString = getNextQuote();
                }
                buf = dString.getBytes();
                // Send the response to the client
                //  at "address" and "port"
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(
                        buf, buf.length, address, port);
                socket.receive(packet);
                double rate = Double.parseDouble(new String(buf).trim());
                socket.send(packet);

                socket.receive(packet);
                double years = Double.parseDouble(new String(buf).trim());
                socket.send(packet);
                socket.receive(packet);
                double loan = Double.parseDouble(new String(buf).trim());
                socket.send(packet);
                double monthlyPayment = loan * (rate / 1200) / 
                        (1 - (Math.pow(1 / (1 + (rate / 1200)), years * 12)));
                socket.send(packet);
                double totalPayment = monthlyPayment * years * 12;
                socket.send(packet);
                jta.append("Interest Rate is " + rate + '\n');
                jta.append("Number Of years " + years + '\n');
                jta.append("Loan Amount is " + loan + '\n');
                jta.append("Monthly payment " + monthlyPayment + '\n');
                jta.append("Total Payment " + totalPayment + '\n');

            } catch (IOException e) {
                e.printStackTrace();
                moreQuotes = false;
            }
        }
        socket.close();
    }

    protected String getNextQuote() {
        String returnValue = null;
        try {
            if ((returnValue = in.readLine()) == null) {
                in.close();
                moreQuotes = false;
                returnValue = "No more quotes. Goodbye.";
            }
        } catch (IOException e) {
            returnValue = "IOException occurred in server.";
        }
        return returnValue;
    }
}

// 客戶端類

public class DgClient extends JFrame {
    // Text field for receiving radius
    private JTextField jtf = new JTextField();
    JTextField interestRate = new JTextField();
    JTextField numberOfYears = new JTextField();
    JTextField loanAmount = new JTextField();
    JButton submit = new JButton("Submit");
    // Text area to display contents
    private JTextArea jta = new JTextArea();
    // Datagram socket
    private DatagramSocket socket;
    // The byte array for sending and receiving datagram packets
    private byte[] buf = new byte[256];
    // Server InetAddress
    private InetAddress address;
    // The packet sent to the server
    private DatagramPacket sendPacket;
    // The packet received from the server
    private DatagramPacket receivePacket;

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

    public DgClient() {
        // Panel p to hold the label and text field
        JPanel fieldsPanel = new JPanel();
        fieldsPanel.setLayout(new GridLayout(3, 3));
        fieldsPanel.add(new JLabel("Anual Interest Rate"));
        fieldsPanel.add(interestRate);
        fieldsPanel.add(new JLabel("Number Of Years"));
        fieldsPanel.add(numberOfYears);
        fieldsPanel.add(new JLabel("Loan Amount"));
        fieldsPanel.add(loanAmount);
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(fieldsPanel);
        topPanel.add(submit);
        add(topPanel, BorderLayout.NORTH);
        add(new JScrollPane(jta), BorderLayout.CENTER);
        submit.addActionListener(new ButtonListener());
        setTitle("Loans Client");
        setVisible(true);
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true); // It is necessary to show the frame here!
        try {
            // get a datagram socket
            socket = new DatagramSocket();
            address = InetAddress.getByName("localhost");
            sendPacket
                    = new DatagramPacket(buf, buf.length, address, 8300);
            receivePacket = new DatagramPacket(buf, buf.length);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                // Initialize buffer for each iteration
                Arrays.fill(buf, (byte) 0);
                double rate = Double.parseDouble(interestRate.getText().trim());
                double years = Double.parseDouble(numberOfYears.getText().trim());
                double amount = Double.parseDouble(loanAmount.getText().trim());
                sendPacket.setData(new Double(rate).toString().getBytes());
                socket.send(sendPacket);
                sendPacket.setData(new Double(years).toString().getBytes());
                socket.send(sendPacket);
                sendPacket.setData(new Double(amount).toString().getBytes());
                socket.send(sendPacket);
                socket.receive(receivePacket);
                double monthlyRepayment = Double.parseDouble(new String(buf).trim());
                socket.receive(receivePacket);
                double totalAmount = Double.parseDouble(new String(buf).trim());
                jta.append("Interest Rate = " + rate + '\n');
                jta.append("Number of years = " + years + '\n');
                jta.append("Loan Amount = " + amount + '\n');
                jta.append("Monthly Repayment = " + monthlyRepayment + '\n');
                jta.append("Total Amount = " + totalAmount + '\n');
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

有人可以幫我造成錯誤的原因。我想它是從客戶端向服務器發送空值。但我不知道原因。

您沒有從DatagramPacket正確提取數據。 您應該使用而不是new String(buf)

new String(packet.getData(), packet.getOffset(), packet.getLength())

發生的任何地方。

我猜它正在發送空值

這里沒有證據顯示空值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM