Browsing 7239 questions and answers with Jon Skeet

SimpleDateFormat: unexpected results and unexpected parse exceptions

I'm having huge difficulties with simple date format. First of all, I know not all tutorials on all sites are actually good, but the fact that all non trivial formats (not...
Jon Skeet
people
quotationmark

And I don't understand why something as simple as: (code snipped) Throws a parse exception. My guess is that it's tripping up over Jun which may not be a valid month abbreviation in your system default locale. I suggest you... more 8/13/2015 3:41:56 PM

people

How do I return a valid input from scanner?

I gave up. I'm trying to figure out why my code is returning the first (incorrect) value. For instance: public static String lastName() { System.out.println("Last Name:...
Jon Skeet
people
quotationmark

You're making a recursive call, but not using the result... so once you come out of the loop, you're just returning the lastName variable from the top-most stack frame If you change this: lastName(); to this: return lastName(); then... more 8/13/2015 2:41:12 PM

people

Find if DateTime is within period, with optional boundaries?

I made the following code to find if the current date is within a specific period, but with optional boundaries: func isElligiblePeriod() { now = new Date(); if...
Jon Skeet
people
quotationmark

It looks like you want: // Check the lower bound, if we have one if (validFrom && now < validFrom) { return false; } // Check the upper bound, if we have one if (validTo && now > validTo) { return... more 8/13/2015 2:09:21 PM

people

Getting last Day of Month XY with Calendar Java

I need to get the last date of a given month, in my case I need to get the last Date of June. My code is following: cal.set(Calendar.DAY_OF_MONTH, ...
Jon Skeet
people
quotationmark

Your code is all over the place at the moment, unfortunately - you're creating new calendars multiple times for no obvious reason, and you're calling Calendar.getActualMaximum passing in the wrong kind of constant (a value rather than a... more 8/13/2015 1:19:44 PM

people

Searching words in a dictionary C#

I have a text file which contains a list of about 150000 words. I loaded the the words into a dictionary and word lookup works fine for me. Now i want to search the dictionary to...
Jon Skeet
people
quotationmark

Now I want to search the dictionary to see if the dictionary contains a word starting from a particular alphabet. That sounds like you want a SortedSet rather than a Dictionary. You can use GetViewBetween to find all the entries in... more 8/13/2015 11:28:02 AM

people

conditional expression "? :" compiles despite branches returning different types

I've started to learn java and I'm confrunting with the following conditional expression: ((1<2)?5:(3<4)) In the book where I've found this example it says that it's a...
Jon Skeet
people
quotationmark

The conditional operator has three operands - the first is the condition, and the second and third are separate "branches" as it were. Which branch is evaluated depends on whether the condition evaluates to true or false. In your case,... more 8/13/2015 10:45:40 AM

people

Create random ints with minimum and maximum from Random.NextBytes()

Title pretty much says it all. I know I could use Random.NextInt(), of course, but I want to know if there's a way to turn unbounded random data into bounded without statistical...
Jon Skeet
people
quotationmark

If you assume that the bits are randomly distributed, I would suggest: Generate enough bytes to get a number within the range (e.g. 1 byte to get a number in the range 0-100, 2 bytes to get a number in the range 0-30000 etc). Use only... more 8/13/2015 10:25:22 AM

people

List.Any get matched String

FilePrefixList.Any(s => FileName.StartsWith(s)) Can I get s value here? I want to display the matched string.
Jon Skeet
people
quotationmark

Not with Any, no... that's only meant to determine whether there are any matches, which is why it returns bool. However, you can use FirstOrDefault with a predicate instead: var match = FilePrefixList.FirstOrDefault(s =>... more 8/13/2015 9:18:14 AM

people

Changing callvirt to call in IL

During some experiments with IL, I attempted to change callvirt calls in an assembly to call methods. Basically what happens is that I have an inheritance chain with member...
Jon Skeet
people
quotationmark

I suspect this blog post is relevant. In particular: Some consider this a violation of privacy through inheritence. Lots of code is written under the assumption that overriding a virtual method is sufficient to guarantee custom logic... more 8/13/2015 6:24:03 AM

people

How to show the complete DateTime value in a Datagridview cell when it is exactly midnight?

I have a DataGridView with a column that holds DateTime values like this: The problem is in the selected row: it actually has the value "10/07/2015 12:00:00 am" but in the...
Jon Skeet
people
quotationmark

You could specify the DefaultCellStyle.Format, but use a "standard format" which will localize appropriately: DefaultCellStyle.Format = "g"; // General date, short time pattern I don't know that that will format midnight "fully", but... more 8/12/2015 10:19:17 PM

people