Browsing 7239 questions and answers with Jon Skeet

Android characters encoding vs Java Characters encoding

In Android I have a text string that I read (its bytes) in this way: String b = "7af18klmo02"; byte[] b=udid.getBytes("UTF-8"); After this, I encrypt the string using a Public...
Jon Skeet
people
quotationmark

This is broken code: criptato = new String(encodeFile, "UTF8"); The contents of encodeFile is not UTF-8-encoded text. It's not text in any encoding - it's arbitrary binary data. Don't convert the byte[] into a String using the String... more 6/26/2015 10:25:05 AM

people

What decides whether a static or object method should be called

Just a general query that came about by someone accidentally giving a variable of one class a name that matched another class where both had a method of the same name, one of...
Jon Skeet
people
quotationmark

After doing this: B A = new B(); ... the identifier A refers to the variable, not the class. To avoid that being a problem, just don't do that... What is going on here and if this is guaranteed could it be used for obfuscation in... more 6/26/2015 10:19:30 AM

people

Why float shows exact representation when declared

I have read many times in articles and MSDN that float (or double) doesn't have exact representation of real world integers or decimal values. Correct ! That is visible when...
Jon Skeet
people
quotationmark

how do they show exact 0.1 while debugging 0.1 isn't the exact value of the float. It happens to be what you specified in the original assignment, but that's not the value of the float. I can see it's confusing :) I suspect the... more 6/26/2015 6:41:04 AM

people

Java.sql.connection object is not working for second time

I have declared Resource annotation in my program for the jboss application server. I am creating connection objects using these annotations. When i am using connection for first...
Jon Skeet
people
quotationmark

You're closing the connection at the end of parseColumnnames - so when you try to use it later on, it won't work... you can't use a closed connection. I would suggest you don't try to cache connections like this. Instead, rely on the data... more 6/26/2015 5:57:03 AM

people

Multiple child elements of XML into one line of combobox

I have some XML that looks similar to this: <SolutionString> <Solutions> <Solution> <ID>1</ID> <Property> ...
Jon Skeet
people
quotationmark

So, for each entry, we have: A Solution element containing: An ID element Some Property elements containing: A Name element A Value element (optional) I would first transform the XML to that structure in memory, and then you can... more 6/25/2015 6:31:15 PM

people

Generating xml file with "&" using java

I need to write <name> Dolce & Gabanna </name> in an xml file using java. However, what gets generated is <name> Dolce &amp; Gabanna </name>. Is...
Jon Skeet
people
quotationmark

I need to write <name> Dolce & Gabanna </name> in an xml file using java. No, you don't. Not unless you're trying to create an invalid file. Whatever code you're using is doing exactly the right thing - any XML parser... more 6/25/2015 4:35:30 PM

people

Is there a way to handle any type of collection, instead of solely relying on Array, List, etc?

This example is for a method called "WriteLines", which takes an array of strings and adds them to an asynchronous file writer. It works, but I am curious if there is an...
Jon Skeet
people
quotationmark

You've been passed an IEnumerable<string> - that means you can iterate over it. Heck, there's even a language feature specifically for it - foreach: foreach (string line in lines) { _queue.Enqueue(line); } Unlike your existing... more 6/25/2015 3:28:33 PM

people

How to convert object type into float where object can be any type of number like int, long, float, double etc

I am having as stream of objects, which can be either type of number like int, short, long, float etc... What is the best way to convert it into a number. Best way could be...
Jon Skeet
people
quotationmark

I believe Convert.ToSingle will be able to handle any "core" numeric type you throw at it. more 6/25/2015 3:17:14 PM

people

Why string.IsNullOrWhiteSpace("\0") is false

I faced a problem where invisible character \0 which is pretty like a 'white space' not considered as white space by the string.IsNullOrWhiteSpace method. I wonder why this...
Jon Skeet
people
quotationmark

U+0000 isn't whitespace, basically. char.IsWhitespace('\0') returns false, it's not listed as whitespace... The null part of IsNullOrWhitespace refers to the string reference itself - not the contents, if that's what you were thinking... more 6/25/2015 2:56:31 PM

people

How to invert a System.Predicate<T>

Let's say I have a Predicate<T>, and I want to call method foo with said predicate returning true, and I want to call method bar with a predicate returning false, how can I...
Jon Skeet
people
quotationmark

You can easily create a method to return an "inverted" predicate - you could even make it an extension method: public static Predicate<T> Invert<T>(this Predicate<T> predicate) { // TODO: Nullity checking return... more 6/25/2015 1:29:34 PM

people