how to clear/empty string value in java -


i have started learning java. learning running simple udp server. program runs fine, if send packet "hello" client returns value hello. however, if send packet 1, value displayed 1ello. have tried clear string null , "" has not worked. how go doing this?

import java.io.*;  import java.net.*;    class udpserver  {     public static void main(string args[]) throws exception        {           datagramsocket serversocket = new datagramsocket(9876);              byte[] receivedata = new byte[1024];              byte[] senddata = new byte[1024];              while(true)                 {                    datagrampacket receivepacket = new datagrampacket(receivedata, receivedata.length);                    serversocket.receive(receivepacket);                    string sentence = new string( receivepacket.getdata());                    system.out.println("received: " + sentence);                    inetaddress ipaddress = receivepacket.getaddress();                    int port = receivepacket.getport();                    string capitalizedsentence = sentence.touppercase();                    senddata = capitalizedsentence.getbytes();                    datagrampacket sendpacket =                    new datagrampacket(senddata, senddata.length, ipaddress, port);                    serversocket.send(sendpacket);                 }        }  }

i have fixed this. sorry ask question.

i moved receivedata & senddata commands while loop retaining data. code now

import java.io.*;  import java.net.*;    class udpserver  {     public static void main(string args[]) throws exception        {           datagramsocket serversocket = new datagramsocket(9876);                while(true)                 {                    byte[] receivedata = new byte[1024];                    byte[] senddata = new byte[1024];                    datagrampacket receivepacket = new datagrampacket(receivedata, receivedata.length);                    serversocket.receive(receivepacket);                    string sentence = new string( receivepacket.getdata());                    system.out.println("received: " + sentence);                    inetaddress ipaddress = receivepacket.getaddress();                    int port = receivepacket.getport();                    string capitalizedsentence = sentence.touppercase();                    senddata = capitalizedsentence.getbytes();                    datagrampacket sendpacket =                    new datagrampacket(senddata, senddata.length, ipaddress, port);                    serversocket.send(sendpacket);                 }        }  }


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -