When I save a phrase in string, it only saves me the first word

0

When I save a sentence in a String, it only saves me the first word.

How can I solve this?

Customer code:

    public static void main(String[] args) {
            String fraseResult;
        try(ServerSocket socketServidor = new ServerSocket(PORT)){
        while(true) {

                System.out.println("Esperant connexions....");

            try (Socket connexio = socketServidor.accept();
                    DataInputStream input = new DataInputStream(connexio.getInputStream());
                    DataOutputStream output = new DataOutputStream(connexio.getOutputStream());) {


                System.out.println("Conexxio acepted...");  

                String fraseRebut = input.readUTF();
                System.out.println("Missatge rebut:" + fraseRebut);
                output.writeUTF(fraseRebut + "des del servidor");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

Server code:

public static void main(String[] args) {
          Scanner lec =new Scanner(System.in);
                int port = 8080;

                try(Socket socket = new Socket(HOST,PORT);
                        DataInputStream input = new DataInputStream(socket.getInputStream());
                            DataOutputStream output = new DataOutputStream(socket.getOutputStream());){


                    String frase=lec.next();
                    String fraseRebut=frase.toUpperCase();
                    output.writeUTF(fraseRebut);
                    fraseRebut = input.readUTF();
                    System.out.println("Missatge rebut:" + fraseRebut);

                } catch (UnknownHostException e) {

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

            }
    }
}
    
asked by Iron Man 17.03.2018 в 14:18
source

1 answer

3

You are currently using the next () method to read the data entry, this operation only reads data until you find a word delimiter such as "space" so it only reads the first word, to read the complete sentence you should use nextLine ()

    
answered by 17.03.2018 / 14:38
source