Friday, 15 March 2013

java - string.equal method not working -


I am new to Java The problem is that I am right now that I am sending a file name from the client to the server. When I print that filename in the server, it prints fine, but it does not match the string that is my hard-coded. Example: I used to write 'Lecture.PDF' as a file name from classic to server 'And then I match the string filename obtained with the string' Lactuation PDF ', it does not match and finally returns false. I hope to return the same method (as it should be).

Here is a code snippet that can display the problem. The 'server' here is the purpose of the socket, and I am reading the file name using the byte array:

  InputStream = faucet; Byte reaction [] = new byte [50]; Is = server.getInputStream (); Is.read (feedback); String str_res = new string (response); System.out.println ("Understand:" + str_res); System.out.println ("Result:" + (response.toString ()). Equals ("lecture pdf"));  

you have to write

  new string (response , "UTF-8"). Equal ("lecture pdf")  

or

  str_res.equals ("lecture.pdf")  

You are resemble your original, unrelated byte array to a string. A byte array is never equal to string because they are of different types, you should also check that the correct number of bytes is changing - if you send 1 byte, then how many letters should be in your output string ? Look at the constructor, and keep track of how many bytes you actually used:

  int bytesRead = is.read (response);  

No comments:

Post a Comment