Double.parseDouble()中的NumberFormatException

时间:2013-04-21 01:54:05

标签: java numberformatexception

我正在尝试创建一个客户端/服务器应用程序,服务器要求客户端编写两个操作数,然后在客户端选择服务器向他发回结果的操作时选择一个操作。

当我想运行我的程序时,我收到了这个错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1lient connectée"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.parseDouble(Double.java:540)
    at Server.main(Server.java:29)

这是我的代码:

* 服务器:*

import java.net.*;
import java.util.*;
import java.io.*;


public class Server {

    public static void main (String[] args){
        try{
            DatagramSocket s = new DatagramSocket(1234);
            byte[] sendBuffer = new byte [1024];
            byte[] recvBuffer = new byte[1024];
            DatagramPacket sendPacket, recvPacket;
            String reply = null;

            //Get the connection declaration from client
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            System.out.println(new String(recvPacket.getData()));


            //Send the first message to client to write the first operand
            sendBuffer = "Entré n1 : ".getBytes();
            sendPacket = new DatagramPacket(sendBuffer,sendBuffer.length,recvPacket.getAddress(),recvPacket.getPort());
            s.send(sendPacket);

            //Get the first operand
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            double n1 = Double.parseDouble(new String(recvPacket.getData()));

            //Send the second message to client to write the second operand
            sendBuffer = "Donner n2 : ".getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, recvPacket.getAddress(), recvPacket.getPort());
            s.send(sendPacket);

            //Get the second operand
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            double n2 = Double.parseDouble(new String(recvPacket.getData()));

            //Send the third message to client to choose the operation
            sendBuffer = "Choisir l'op : \n1-Addition \n2-Soustraction \n3-Multiplication \n4-Division \nVotre choix : ".getBytes();
            sendPacket= new DatagramPacket(sendBuffer, sendBuffer.length, recvPacket.getAddress(), recvPacket.getPort());
            s.send(sendPacket);

            //Get the number of operation
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            reply = new String(recvPacket.getData());

            //Traitement
            String res = null;
            switch(reply){
                    case "1" :
                        res = String.valueOf(n1 + n2);
                        break;
                    case "2" :
                        res = String.valueOf(n1 - n2);
                        break;
                    case "3" :
                        res = String.valueOf(n1*n2);
                    case "4" :
                        res = (n2 == 0) ? "Division sue zéro" : String.valueOf(n1/n2);
                        break;
                    default :
                        res = "Erreur";
            }       

            //Send the result of the operation to the client
            sendBuffer = ("Resultat : "+res).getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, recvPacket.getAddress(), recvPacket.getPort());
            s.send(sendPacket);

        }catch(IOException e){
            e.printStackTrace();
        }
    }

}

客户:

import java.net.*;
import java.util.*;
import java.io.*;


public class Client {

    public static void main (String[] args) throws UnknownHostException{
        DatagramSocket s;
        byte[] sendBuffer = new byte[1024];
        byte[] recvBuffer = new byte[1024];
        DatagramPacket sendPacket, recvPacket;
        String reply = null;
        final InetAddress ADRSS = InetAddress.getByName("localhost");
        final int PORT = 1234;
        Scanner cn = new Scanner(System.in);
        try{
             s = new DatagramSocket();

             //Declare connection to server
            sendBuffer = "Client connectée".getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, ADRSS, PORT);
            s.send(sendPacket);

            //Receive the first message from server
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            System.out.println(new String(recvPacket.getData()));

            //Send the answer for the first Message to server (first operand)
            sendBuffer = cn.nextLine().getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, ADRSS, PORT);
            s.send(sendPacket);

            //Receive the second message from server
            recvPacket = new DatagramPacket(recvBuffer,recvBuffer.length);
            s.receive(recvPacket);
            System.out.println(new String(recvPacket.getData()));

            //Send the answer for the second Message to server (second operand)
            sendBuffer = cn.nextLine().getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, ADRSS, PORT);
            s.send(sendPacket);

            //Receive the third message from server
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            System.out.println(new String(recvPacket.getData()));

            //Send the answer for the third Message to server (operation)
            sendBuffer = cn.nextLine().getBytes();
            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, ADRSS, PORT);
            s.send(sendPacket);

            //Receive the result from server (result of operation)
            recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
            s.receive(recvPacket);
            System.out.println(new String(recvPacket.getData()));
        }catch(IOException e){
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

double n1 = Double.parseDouble(new String(recvPacket.getData()));

这一行导致错误,因为您从客户端发送String并期望在服务器上加倍。您也可能面临与变量n2相同的问题。所以,只需将它们视为String,就会很好。