Browsing 7239 questions and answers with Jon Skeet

How can I print a byte along with a string?

I have a byte: byte STX = 0x02; Printing it as it is with printf returns 0x02 which is what I want: System.out.printf("0x%02X", STX); However, I want to concatenate it with...
Jon Skeet
people
quotationmark

Unfortunately this is effectively a copy of an existing answer, which was downvoted... but as your comment now indicates that you'd welcome such an option... You can use printf to format your whole string: System.out.printf("Found 0x%02X... more 2/1/2014 6:42:52 PM

people

How to force a runtime constant to be a compile time constant?

So I am working on a chemistry based project and ran into this tricky problem. I have a bunch of functions doing chemistry type calculations and want to pass avogadros number as a...
Jon Skeet
people
quotationmark

You can't, in general. Anything which involves a method call is not going to be a compile-time constant, as far as the compiler is concerned. What you can do is express a double literal using scientific notation though: public const... more 2/1/2014 5:59:33 PM

people

Generic parameter: How to check type

If I have the following function: Private Function AddObject(Of T As MyBaseClass)(pos As PointF, sz As SizeF) As T End Function How can I check the type of T inside the...
Jon Skeet
people
quotationmark

If you only need an exact match you can use: GetType(T) Is GetType(Rect) If you want to match with inheritance, you can use: GetType(Rect).IsAssignableFrom(GetType(T)) Although to be honest, if you have to handle each subclass... more 2/1/2014 4:55:52 PM

people

Validate Base64 string

I am converting an image to base64 on a html5 mobile client and posting the string to my webapi service. I try to convert the received string back to an image and I get the...
Jon Skeet
people
quotationmark

My guess is that you're including the "data:image/png;base64," part at the start of the string - you need to strip that first. You don't need to do anything else - with the text in the pastebin, Convert.FromBase64String handles everything... more 2/1/2014 9:13:07 AM

people

What date time should I put into the database

We are designing a small application where people can leave comments. The users of this application could be anywhere in the world. Comments from one can be viewed by everyone...
Jon Skeet
people
quotationmark

You almost certainly want to store the data in UTC. You then display the time to the user in their local time zone, so it'll never appear to be in the future. Different people around the world will see different times for the same comment,... more 2/1/2014 9:07:44 AM

people

Unable to send a string from app to servlet using outputStreamWriter()

I'm trying to send a small String over from my app to a Java servlet: //Send user mobile number to the server. EditText cinMobile = (EditText)findViewById(R.id.mobile_no); String...
Jon Skeet
people
quotationmark

I'm not sure what is wrong here but the error implies that it is not receiving any string. No, the error implies that you're trying to create an array with a negative length. That implicates these lines: int length =... more 2/1/2014 8:59:18 AM

people

Short|quick int values comparison

I learned about terminary expression, but what I want is a little different. I have the following: int MODE = getMyIntValue(); I do comparison as the following: if(MODE == 1...
Jon Skeet
people
quotationmark

Well, if you don't mind the boxing hit, you could use a set which you prepared earlier: // Use a more appropriate name if necessary private static final Set<Integer> VALID_MODES = new HashSet<>(Arrays.asList(1, 2,... more 1/31/2014 9:04:27 PM

people

Load xml row into 2D array

So I have this xml <document> <Month> <Depth>-0,25</Depth> <October>0,95</October> <November>-0,90</November> ...
Jon Skeet
people
quotationmark

If you're happy with a jagged array (an array of arrays) then LINQ makes it fairly easy: XDocument doc = XDocument.Load(...); var array = doc.Root .Elements("Month") .Select(month =>... more 1/31/2014 9:00:45 PM

people

Ordering by attribute XML file with nested elements

I would like to order my XML file, but I'm having troubles in understanding how. I noticed that a lot of suggestions are similar to this case. var bookstore =...
Jon Skeet
people
quotationmark

You're not really trying to order all the car elements - you're trying to order each group of elements. It's probably simplest just to use ReplaceNodes for each difficulty element: foreach (var difficulty in... more 1/31/2014 4:57:20 PM

people

Java executes arithmetic Expression wrong?

I don`t understand how Java is progressing this arithmetic expression int x = 1; int y = 1; x += y += x += y; System.out.println("x=" + x + " y=" + y); With Java I get x = 4...
Jon Skeet
people
quotationmark

This is the nasty part, obviously: x += y += x += y; This is executed as: int originalX = x; // Used later x = x + y; // Right-most x += y y = y + x; // Result of "x += y" is the value stored in x x = originalX + y; // Result of "y +=... more 1/31/2014 4:46:10 PM

people