NumberFormatException в Double.parseDouble()
Я пытаюсь создать клиент-серверное приложение, сервер просит клиента написать два операнда, а затем выбрать операцию, когда клиент выбирает операцию, которую сервер отправляет ему в ответ.
когда я хочу запустить свою программу, я получил эту ошибку:
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 ответ
double n1 = Double.parseDouble(new String(recvPacket.getData()));
Эта строка вызывает ошибку, так как вы отправляете String с клиента и ожидаете удвоения на сервере. Вы, вероятно, столкнетесь с той же проблемой с переменной n2. Так что просто относитесь к ним как к String, и все будет хорошо.