Browsing 7239 questions and answers with Jon Skeet

How to cast String to byte array?

I have a String, which contains the byte array's String value. How could I convert this String to byte array? How I tried: String stringValue="33321232"; //the bytes in...
Jon Skeet
people
quotationmark

I have a String, which contains the byte array's String value. This is unclear to start with. If you've converted binary data to text, how have you done that? That should guide you as to how you convert back. For example, if you've... more 11/7/2013 11:36:54 AM

people

Call a generic method and set the generic type at runtime

In the example bellow, is it possible to set the Classname type parameter dynamically? UpdateAndSave<Classname>>().Execute(sql)
Jon Skeet
people
quotationmark

Well you can call it by reflection, yes - using MethodInfo.MakeGenericMethod to provide the type arguments: var method = typeof(Whatever).GetMethod("UpdateAndSave"); var genericMethod =... more 11/7/2013 11:24:06 AM

people

Using block: object initialization compiled into try block

In my project I have an object whose constructor can throw. So the code I'm using all across is as follows: MyObject obj = null; try { obj = new MyObject(); ...
Jon Skeet
people
quotationmark

The question is: can I somehow make CLR put object's constructor into a try block? No. Or rather, it's pointless to do so, from a resource management perspective. If an exception is thrown by the constructor, then there won't be a... more 11/7/2013 9:02:21 AM

people

How to build FileInputStream object with byte array as a parameter

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object. I dont want to create a file instead pass data...
Jon Skeet
people
quotationmark

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object. But you don't have a file. You have some data in memory. So a FileInputStream is inappropriate -... more 11/7/2013 7:12:04 AM

people

copying one Array value to another array

I have 2 array of object. 1st array of object have property which I want to copy to other array. 1st array of object HotelRoomResponse[] hr=new HotelRoomResponse[100]; 2nd...
Jon Skeet
people
quotationmark

You can't just project an array like that. You effectively have to loop - although you don't need to do that manually in your own code. LINQ makes it very easy, for example: RateInfos[] rt = hr.Select(x => x.RateInfo).ToArray(); Or... more 11/7/2013 7:00:48 AM

people

Storing '\n' in Sql using Java

I want to store a string containing '\n' in Sql using Java. But the problem is in the database instead of \n a new line is getting stored. e.g the string is "abcd\nefgh" then the...
Jon Skeet
people
quotationmark

But the problem is in the database instead of \n a new line is getting stored. Well it sounds like the string itself probably contains a newline. If you write: String x = "a\nb"; in Java, then that is a string with 3... more 11/7/2013 6:57:11 AM

people

How to subtract one DateTime from Another and return a double of hours passed

So I have two DateTimes: date1 = 1/1/2000 12:00:00 AM date2 = 1/1/2000 12:30:00 AM How can I subtract date2 from date1 and return a double of .5?
Jon Skeet
people
quotationmark

You can subtract one DateTime from another using the - operator (or use the Subtract method) to get a TimeSpan, then use TimeSpan.TotalHours: DateTime start = new DateTime(2000, 1, 1, 0, 0, 0); DateTime end = new DateTime(2000, 1, 1, 0,... more 11/6/2013 7:43:58 PM

people

Why does changing the sum order returns a different result?

Why does changing the sum order returns a different result? 23.53 + 5.88 + 17.64 = 47.05 23.53 + 17.64 + 5.88 = 47.050000000000004 Both Java and JavaScript return the same...
Jon Skeet
people
quotationmark

Maybe this question is stupid, but why does simply changing the order of the elements affects the result? It will change the points at which the values are rounded, based on their magnitude. As an example of the kind of thing that... more 11/6/2013 6:56:32 PM

people

Why can not Java multi catch deal with types that is related by subclassing?

Here is a piece of code that shall not compile: void multiCatch() { try { throwIOFile(); } // FileNotFoundException extends IOException, hence this //...
Jon Skeet
people
quotationmark

But what would be the harm in making my code snippet legal? Surely there has to be a harm for a practice to become illegal? It's confusing code - you can simplify the code by just catching IOException, whereas it looks like you really... more 11/6/2013 5:47:51 PM

people

Bufferedreader and files encoded through ASCII

I've got to read a file (it contains text) encoded through ASCII. I decided to use Bufferedreader class. I know that, when I deal with files encoded through UTF-8, I can specify...
Jon Skeet
people
quotationmark

No, you should specify an encoding using InputStreamReader - BufferedReader doesn't let you specify the encoding. For ASCII files you're likely to be okay with the platform default encoding (most defaults are compatible with ASCII), but... more 11/6/2013 5:15:15 PM

people