Browsing 7239 questions and answers with Jon Skeet

how does this small c# code work

I joined a new team that automates using c# and selenium. I cant follow how this line works: driver.FindElement(Elements.OkLink).click() I know about why driver and...
Jon Skeet
people
quotationmark

Is LinkText a method. Yes. I thought methods are similar to functions. That's right. How is ClassName MethodName is used here. In the declaration? That just indicates what the method returns. So to pull this declaration... more 12/14/2013 10:57:21 AM

people

How get properties for not collection property

I have a Person class that inherits EntityBase: public class Person : EntityBase { virtual public string FirstName { get; set; } virtual public string LastName {...
Jon Skeet
people
quotationmark

You can filter by the return type of the property. I suspect you want to filter out anything that implements IEnumerable, but isn't string (which implements IEnumerable<char>, but which you want to keep). So something like: var... more 12/14/2013 9:24:20 AM

people

Convert int days to months and days using joda

Say I have 203 days. I want to convert that number to the string x months y days from today. How do I do that using joda time? (of course 203 is just an example, use z if that...
Jon Skeet
people
quotationmark

EDIT: Working out the period starting at a particular date is pretty easy with Joda Time. For example: public Period getMonthsAndDays(int days, LocalDate start) { LocalDate end = start.plusDays(days); return new Period(start, end,... more 12/13/2013 11:30:37 PM

people

What is object field initialization and constructor order in Java

I ended up the following scenario in code earlier today (which I admit is kinda weird and I have since refactored). When I ran my unit test I found that a field initialization...
Jon Skeet
people
quotationmark

Yes, in Java (unlike C#, for example) field initializers are called after the superclass constructor. Which means that any overridden method calls from the constructor will be called before the field initializers are executed. The... more 12/13/2013 11:01:39 PM

people

new vs override keywords

I've got a question concerning polymorphic methods. I've got two classes: the base class with the non-virtual method Foo( ) which calls its virtual method Foo (int i) (like this:...
Jon Skeet
people
quotationmark

(When using new.) It doesn't even get to the new Foo(int i) method. Yes it does - but it executes the BaseClass implementation of Foo(int) because it's not overridden in the derived class. That's the whole point of new - it's... more 12/13/2013 10:56:57 PM

people

IllegalMonitorStateException not thrown where it should

While studying for OJP I came to the topic of Thread wait() method and so on, According to the book, this portion of code should throw IllegalMonitorStateException since the...
Jon Skeet
people
quotationmark

Yes, an exception is being thrown, for exactly the reason you describe - you're just catching it and swallowing it: catch(Exception x){} Don't do that. Never do that. Just doing this: catch (Exception x) { ... more 12/13/2013 10:53:03 PM

people

No enclosing instance of (class here) is accessible

I have a class called GreenhouseControls that has a bunch of classes built into it such as: public class ThermostatNight extends Event { public ThermostatNight(long...
Jon Skeet
people
quotationmark

Your classes are currently inner classes, which need to be constructed in the context of instances of the containing class. Your options are: Specify an instance of the outer class when constructing an instance of the inner class Declare... more 12/13/2013 10:23:27 PM

people

Why does this lead to an ArrayIndexOutOfBoundsException?

There is something that doesn't quite make sense to me. Why does this: public static int[] countNumbers(String n){ int[] counts = new int[10]; for (int i = 0; i < n.length();...
Jon Skeet
people
quotationmark

This is the problem: counts[n.charAt(i)]++; n.charAt(i) is a character, which will be converted to an integer. So '0' is actually 48, for example... but your array only has 10 elements. Note that the working version isn't subtracting... more 12/13/2013 3:15:07 PM

people

Find highest,lowest and average in a loop

I know it's probably something small that I'm doing wrong. I want it to print the lowest number etc. at the end of the loop, but it always prints zero as lowest. How do I get it...
Jon Skeet
people
quotationmark

Your highest and lowest variables both start at 0. So if your numbers are all negative, you'll get a highest of 0, incorrectly. If your numbers are all positive, you'll get a lowest of 0, incorrectly. Just start off with highest =... more 12/13/2013 1:40:34 PM

people

How can I convert a string into a GZIP Base64 string with ByteArrayOutputStream?

I need to convert string to gzip base64. I have this code import java.util.zip.GZIPOutputStream; import org.apache.commons.codec.binary.Base64OutputStream; public class Test { ...
Jon Skeet
people
quotationmark

You're calling os.toByteArray() before you've closed gzip and b64os - which means they've almost certainly got buffered data. (Aside from anything else, a base64 stream has to wait until it's closed to write out any padding.) Just move... more 12/13/2013 11:16:44 AM

people