简体   繁体   English

NumberFormatException解析为两倍

[英]NumberFormatException parsing to double

When i am sending values from the Client to the server i am getting an exception like this below. 当我从客户端向服务器发送值时,出现如下异常。 The statement in the server dString = new Date().toString(); 服务器中的语句dString = new Date().toString(); is getting executed instead of processing the datagram packet. 正在执行而不是处理数据报包。 Could some please give your inputs to correct the server or my client to get the accurate output. 能否请您提供一些输入信息以更正服务器或我的客户端,以获得准确的输出。

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)

.//Server Class .//服务器类

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

. // Client Class // 客户端类

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

Could someone please assist me what was causing the error.I guess it is sending null values from the client to server.But i donot know the reason for it. 有人可以帮我造成错误的原因。我想它是从客户端向服务器发送空值。但我不知道原因。

You're not extracting the data from the DatagramPacket correctly. 您没有从DatagramPacket正确提取数据。 Instead of new String(buf) you should be using 您应该使用而不是new String(buf)

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

everywhere it occurs. 发生的任何地方。

I guess it is sending null values 我猜它正在发送空值

There are no null values in evidence here. 这里没有证据显示空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM