Browsing 7239 questions and answers with Jon Skeet

Parsing XML by position using XDocument

I have some XML from a third party that looks something like this: <Breakfasts> <Name>Cornflakes</Name> <Description>Just add milk</Name> ...
Jon Skeet
people
quotationmark

Ick - what a horrible format. I'd probably do something like: var list = doc.Descendants("Name").Select(MakeBreakfast).ToList(); ... static Breakfast MakeBreakfast(XElement nameElement) { string name = (string) nameElement; var... more 6/25/2015 11:37:18 AM

people

Get hour in current timezone from unix time

I am trying to retrieve a converted "hour" integer for a user's time zone with GMT unix time. My code works SOME of the time, though for example, it was 9:00pm on the east coast...
Jon Skeet
people
quotationmark

You don't need to set the time zone - it's default by default, as it were. And calling setTimeInMillis twice is pointless. So just: Calendar calendar = calendar.getInstance(); calendar.setTimeInMillis(unixTimestamp * 1000L); int hour =... more 6/25/2015 5:50:42 AM

people

Why is there a "==" inside String.equals?

Why Java is comparing (this == another String) inside equalsIgnoreCase method for checking a string insensitive? Also, String equals is comparing (this == another String) to...
Jon Skeet
people
quotationmark

Why Java is comparing (this == another String) inside equalsIgnoreCase method for checking a string insensitive? It's an optimization. If the reference passed in is exactly the same as this, then equals must return true, but we don't... more 6/25/2015 5:39:01 AM

people

Casting SQL Date Value using Convert.ToDateTime in C# is failing

To start, the error I'm getting is as follows: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. What I've done is returned a...
Jon Skeet
people
quotationmark

Basically, you shouldn't be converting the values to strings and back in the first place. Wherever possible, avoid string conversions unless that's an essential part of what you're trying to do. In your case, you're really interested in... more 6/24/2015 8:06:10 PM

people

How to determine method caller type at run time?

I wish to know if there is a way for a method to determine, at run time, the type that invokes it. [UPD2] For this particular case I'm interested in the Type of the caller only,...
Jon Skeet
people
quotationmark

Well, you could use reflection to access the stack trace - but that's slow, and in some cases I wouldn't be surprised if the JIT skipped some stack frames due to inlining. One option is to make the methods internal, and only include the... more 6/24/2015 7:57:47 PM

people

Object referencing confusion

I am getting a NullPointerException on the MainClass.ob1.noOfVerts variable in the following code: import java.awt.*; import javax.swing.*; public class drawr extends...
Jon Skeet
people
quotationmark

The object is instantiated here: public static displayObject **ob1**, ob2, ob3; No, that's not instantiating anything. It's declaring a variable, but it's just got its default value of null. At some point you need to assign a... more 6/24/2015 7:42:26 PM

people

Why do I get an “ArrayIndexOutOfBounds” error in this program?

I wrote this program to initialize and create a deck of cards, to shuffle it, and then to deal out N hands of cards. In spite of the fact that compilation proceeds without a hitch...
Jon Skeet
people
quotationmark

The problem is in your shuffling code: for (int i = 0; i < deck.length; i++) { int r = i + (int)(Math.random() * (N-1)); String t = deck[i]; deck[i] = deck[r]; deck[r] = t; } There's no guarantee that r will be in... more 6/24/2015 12:24:51 PM

people

How to test the right associativity of conditional operator in c#

I wrote the following code. static void Main(string[] args) { var xyz = 10 < 11 ? 12 : 5 < 21 ? getValue() : 15; Console.WriteLine(xyz); ...
Jon Skeet
people
quotationmark

This: var xyz = 10 < 11 ? 12 : 5 < 21 ? getValue() : 15; is treated as: var xyz = 10 < 11 ? 12 : (5 < 21 ? getValue() : 15); So we have a conditional expression with: Condition: 10 < 11 First branch: 12 Second... more 6/24/2015 6:02:57 AM

people

PHP datetime createFromFormat fails. why?

Ok- here's what Im doing: $dateString = "2015-06-12 06:01 am"; $dtUtc = new \DateTimeZone("UTC"); $dt = \DateTime::createFromFormat('yyyy-mm-dd h:i a', $dateString, $dtUtc); if...
Jon Skeet
people
quotationmark

Judging by the documentation, you want: DateTime::createFromFormat('Y-m-d g:i a', $dateString, $dtUtc) Note that Y is "4 digit year", m is "zero-padded month", d is "zero-padded day", and g is "zero-padded hour". Admittedly it's quite... more 6/23/2015 5:51:05 PM

people

Opening InputStreamReader in the middle of UTF 8 stream

I am using a seekable InputStream which returns the stream to me at a specific position. The underlying data in the stream is encoded with UTF-8. I want to open this stream using...
Jon Skeet
people
quotationmark

Assuming you can reposition the stream whenever you want, you can simply read bytes while the top two bits are "10". So something like: // InputStream doesn't actually have a seek method, but I'll assume you're using // a subclass which... more 6/23/2015 4:25:08 PM

people