Browsing 7239 questions and answers with Jon Skeet

How can I improve by optimizing this piece of Java Code

I have been trying to figure out if I can use some other sort of Data Structures for this problem. I managed to somehow solve it, but still lacking a lot of good quality code. I...
Jon Skeet
people
quotationmark

It sounds like you just want: String[] outputString = new String[inputString.length]; for (int i = 0; i < inputString.length / 2; i++) { outputString[i * 2] = inputString[i]; outputString[i * 2 + 1] = inputString[i +... more 9/25/2013 10:46:23 PM

people

Error: Before start of result set in Java

I know this would be a foolish question to ask but still i need to do this. This is a basic program in java application where I want to use 3 queries simultaneously to print the...
Jon Skeet
people
quotationmark

This is the problem: while( rs1.next() || rs2.next() || rs3.next() ) If rs1.next() returns true, rs2.next() and rs3.next() won't be called due to short-circuiting. So rs2 and rs3 will both be before the first row. And if rs1.next()... more 9/25/2013 10:03:45 PM

people

Strange Behavior with Pictureboxes

I am experiencing strange behavior with PictureBoxes and have narrowed down a test case. I have four PictureBoxes on my test form. Two have a background color set ... one red,...
Jon Skeet
people
quotationmark

I suspect this is the problem: this.bluePictureBox.Location = this.pictureBox2.Location; You're setting the location of the blue picture box within picture box 2 to be the location of picture box 2 relative to the container. I suspect... more 9/25/2013 9:59:06 PM

people

What's the standard to make an event that doesn't pass any information along?

I've got an event that literally just lets the user know something happened. They have to manually take action for that event, as no data is passed from it. However, I'm not sure...
Jon Skeet
people
quotationmark

The idiomatic approach would be to just use EventHandler. Pass in an appropriate sender if you have one or null otherwise, and EventArgs.Empty. I know it's somewhat crazy, but that's the convention. Bear in mind that plain EventArgs has... more 9/25/2013 9:48:30 PM

people

Java Dates and Standard Formats

As per this C# code from Mircrosoft there is a standard date format such as this: // R: Sun, 15 Jun 2008 21:15:07 GMT My C# code uses "r" like...
Jon Skeet
people
quotationmark

How can I create a standard date format like this in Java? Look at the documentation for the r format: The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". When this standard format specifier is used, the formatting... more 9/25/2013 7:34:32 PM

people

Overloaded method selection logic

Given the following overloaded methods: public string Test(long item) { return "Test with a long was called!"; } public string Test(int item) { return "Test with an int...
Jon Skeet
people
quotationmark

Why is the value result equal to "Test with an int was called!", instead of "Test with an object was called!"? The conversion to int is "better" than the conversion to object, so the overload taking int is "better" than the one taking... more 9/25/2013 5:55:54 PM

people

Is it possible to dynamically specify the type of event when using AddHandler?

I am trying to write a clever little function here that will add a specified delegate as an event handler to all the controls in a collection for any dynamic event. What I'm...
Jon Skeet
people
quotationmark

Two options: Specify a "subscription delegate" via a lambda expression. I wouldn't like to guess at what this would look like in VB, but in C# it would be something like: (control, handler) => control.MouseEnter += handler; Then... more 9/25/2013 5:48:35 PM

people

Java String Index Out of Bounds Exception

I'm getting the following exception on the line where head is assigned and I'm not sure why: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index...
Jon Skeet
people
quotationmark

The problem is that you're using the result of s.charAt(commaSpot) as the second argument - rather than commaSpot itself. You want: head = s.substring(0, commaSpot); tail = s.substring(commaSpot, s.length()); You don't care about the... more 9/25/2013 4:52:21 PM

people

SimpleDateFormat: hours are HH after parsing

I have a String which is 2013-8-25, 16:33, now I need to convert it into Date. For this purpose I use: Date mDate = new SimpleDateFormat("yyyy-MM-dd, HH:mm").parse("2013-8-25,...
Jon Skeet
people
quotationmark

I strongly suspect that you're running into a compatibility issue with DateFormat. In particular, from the documentation: The format methods in this class implement a subset of Unicode UTS #35 patterns. The subset currently supported... more 9/25/2013 1:59:16 PM

people

Namespace semantic differences

I see most of the types in .NET framework are spread across 3 different namespaces (may be more), one start with Microsoft, other with System and the third with Windows. For...
Jon Skeet
people
quotationmark

Microsoft.* namespaces are typically .NET namespaces for features which are specific to Windows, e.g. registry access System.* namespaces are "normal" .NET system namespaces Windows.* namespaces are typically part of Windows Runtime aka... more 9/25/2013 1:51:08 PM

people