Browsing 7239 questions and answers with Jon Skeet

Returning null if multiple instances are found

I have an IEnumerable and a predicate (Func) and I am writing a method that shall return a value if only one instance in the list matches the predicate. If the criteria is matched...
Jon Skeet
people
quotationmark

The "slightly verbose" option seems reasonable to me, and can easily be isolated into a single extension method: // TODO: Come up with a better name :) public static T SingleOrDefaultOnMultiple<T>(this IEnumerable<T>... more 3/11/2014 1:12:19 PM

people

Determine if a JTextField contains an integer

I'm making a word guessing game. The JTextField can't be empty, can't be longer or smaller than 5 and cannot contain numbers. How do I do the last part? @Override public void...
Jon Skeet
people
quotationmark

Rather than explicitly checking for numbers, I would suggest whitelisting characters which are allowed. Would letters with accents be allowed, for example? Both upper and lower case? Punctuation? Once you've worked out your requirements,... more 3/11/2014 11:18:29 AM

people

Avoiding code duplication for similar backgroundworkers used in dissimilar classes

I have several classes that are all using backgroundWorker threads to run logic while updating a status bar on my main form. I'm finding that a lot of the set up and reporting...
Jon Skeet
people
quotationmark

The final option seems reasonable to me - and you can avoid the duplication of the final two lines, and even the one that you missed (the DoWork event): public static BackgroundWorker CreateBackgroundWorker (DoWorkEventHandler... more 3/11/2014 10:22:53 AM

people

Class does not contain a constructor that takes 0 arguments

I have these 2 classes that are respectively called: Malicious and MaliciousSmall: Code of Malicious: using System; using System.Collections.Generic; using System.Linq; using...
Jon Skeet
people
quotationmark

The error is telling you that the Malicious constructor can't find a suitable constructor to chain to in its base class (MaliciousSmall). The initial problem is that your derived class constructor implicitly contains this... more 3/11/2014 9:24:55 AM

people

Rounding a double and grouping it?

I'll explain this the best I can. Let's say I get a user input and that is: 245, I want this to be rounded down to 200 then divided by 100 and then finally +1 so the outcome is 3...
Jon Skeet
people
quotationmark

Why not just divide it by 100 to start with? That will do the rounding (always towards 0) if you start off with an integer: int zone = (input / 100) + 1; There's no need to get into non-integer arithmetic at all here. If the user input... more 3/11/2014 8:54:36 AM

people

JodaTime Check if LocalTime is after now and now before another LocalTime

I am trying to check if the currenttime is after a startLocalTime and before another endLocalTime which is the start time plus 11 hours. This works fine if the start time is 11:00...
Jon Skeet
people
quotationmark

Basically you need to check whether your startTime and endTime are inverted (i.e. if endTime is before startTime). If it is, that must be a "night-time" interval, and you should just treat it as if they were the other way round, and invert... more 3/10/2014 8:17:44 PM

people

Select statement between two java.sql.Date

my code is: java.sql.Date fromDate= new java.sql.Date(date1); java.sql.Date toDate= new java.sql.Date(date2); String select = "SELECT * FROM Table WHERE Date between...
Jon Skeet
people
quotationmark

First, stop building SQL like that. It's vulnerable to SQL injection attacks, conversion issues (which is probably the problem here) and it's hard to read. Use parameterized SQL instead: // TODO: Close the statement, e.g. using a... more 3/10/2014 7:29:30 PM

people

Add byte order mark to a string via StringBuilder

How can I add a byte order mark to a StringBuilder? (I have to pass a string to another method which will save it as a file, but I can't modify that method). I tried this: var...
Jon Skeet
people
quotationmark

Two options: Don't include the byte order mark in your text at all... instead use an encoding which will automatically include it Include it as a character in your StringBuilder: sb.Append('\uFEFF'); // U+FEFF is the byte-order mark... more 3/10/2014 5:13:09 PM

people

Enum with negative/gap value in Java

I have a problem with enum in Java. I have an enum that starts from -1: public enum AipType { Unknown(-1), None(0), AipMod(1), AipNoMod(2); ...
Jon Skeet
people
quotationmark

Just have a Map<Integer, AipType> instead of using values(), and expose access to it via a method: public enum AipType { UNKNOWN(-1), NONE(0), MOD(1), NO_MOD(2); private static final Map<Integer, AipType>... more 3/10/2014 4:22:17 PM

people

Why is entity &eacute; not valid whereas entity &lt; is?

I'm looking at what the xml resolver System.Xml.Resolvers.XmlPreloadedResolver brings to the table in terms of dtds and i'm stumped by the fact that the entity &lt; is...
Jon Skeet
people
quotationmark

&lt; is a fundamental entity defined in the XML specification itself; &eacute; isn't. That's why you're seeing the difference in behaviour. (So I'd expect &amp;, &gt;, &apos; and &quot; to work too.) See... more 3/10/2014 1:51:35 PM

people