Browsing 7239 questions and answers with Jon Skeet

Return key for a value found in Hashtable

I know how to find a value for a corresponding key in Hashtable but how do I find what key is attached to a specific value? I wrote a function that loops through a String and...
Jon Skeet
people
quotationmark

containsValue has to iterate over all the entries in the hash table anyway - so just change your code so that you do that: for (Map.Entry<Integer, String> entry : hashtable) { if (word.equals(entry.getValue()) { // Use... more 1/31/2018 10:43:55 AM

people

Difference in ZoneDateTime precision between Windows and Unix

I execute the following code and I got a different precision between Windows and Unix (macOs). import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public...
Jon Skeet
people
quotationmark

I suspect you just happened to run at an instant where the time was on a 10ms boundary on the Mac. Here's some sample code to try: import java.time.*; import java.time.format.*; class Test { public static void main(String[] args)... more 1/31/2018 10:18:42 AM

people

Cast generic Collection to List once, then RemoveAt multiple times

I have a generic collection of objects as a property of an object. This data comes from a sql query and api. I want to use the RemoveAt method to remove some of them efficiently....
Jon Skeet
people
quotationmark

Just do it in three statements instead of two, using a local variable: var list = obj.stuckArray.ToList(); list.RemoveAt(1); obj.stuckArray = list; That way the type of stuckArray doesn't matter as much: you only need to be able to call... more 1/30/2018 5:04:00 PM

people

Constructor chaining and preparing arguments before calling this(aguments)

I am making a Yahtzee game. I want to supply a constructor for different cases. Suppose you couldn't be bothered to supply the names of the players that you want to create a new...
Jon Skeet
people
quotationmark

Yes, there's fairly simple way around it, at least in this case: create a static method which will prepare the constructor argument for you. Call that from the this expression: public YahtzeeGame(int numberOfPlayers) { ... more 1/30/2018 3:38:52 PM

people

Calling getTime changes Calendar value

I'm trying to get the sunday of the same week as a given date. During this I ran into this problem: Calendar calendar = Calendar.getInstance(Locale.GERMANY); calendar.set(2017,...
Jon Skeet
people
quotationmark

Basically, the Calendar API is horrible, and should be avoided. It's not documented terribly clearly, but I think I see where it's going, and it's behaving as intended in this situation. By that I mean it's following the intention of the... more 1/30/2018 10:31:10 AM

people

.NET target framework compatibility and the compiler on .NET 4.5 or higher

I'm fairly experienced in .NET development but today I was forced to wrap my head around something I'd never thought about before: How do the installed .NET Framework, the .NET...
Jon Skeet
people
quotationmark

How does the compiler know that I can't use SystemDefault on .NET 4.5 but can use it on 4.7? Is this done via some kind of API file known to the compiler? Yes, I'd expect it to be done via the reference assemblies. A reference... more 1/29/2018 2:43:58 PM

people

Difference between two dates and working out if the person is over 18 or under 18 c#

I'm going to have two dates. A Date of birth And a Date of event I want to calculate if the time passed between date of birth and event date is less than or greater than 18...
Jon Skeet
people
quotationmark

The example you gave is a reasonably easy one - but there are harder ones. Consider this: Date of birth: Feb 29th 1976 Event date: Feb 28th 1994 If you add 18 years to the date of birth, you'll get Feb 28th 1994 - the event date. That... more 1/29/2018 11:17:01 AM

people

Get elements from a list based on enum

I have an enum indicating the eligibility of a spawn. protected enum SpawnEligibility { Worst, Ok, Optimal } Then, I have a function to return the...
Jon Skeet
people
quotationmark

It sounds to me like you want to group by eligibility, then order those groups by their key (descending), then take the first group: var bestSpawnGroup = _spawns .GroupBy(GetEligibility) .OrderByDescending(g => g.Key) ... more 1/28/2018 9:09:09 AM

people

Stream was not readable

I have code below that read ftp response stream and write data to two different files (test1.html & test2.html). The 2nd StreamReader throw a stream was not readable error....
Jon Skeet
people
quotationmark

You can't read from the stream twice. After this call: responseStream.CopyTo(output); ... you've already read all the data in the stream. There's nothing left to read, and you can't "rewind" the stream (e.g. seeking to the beginning)... more 1/28/2018 7:51:20 AM

people

Is there a way I can check every item in a list and produce a result based on that check?

I have a variable: public static List<CardSetWithWordCount> cardSetWithWordCounts; Where the class looks like this; public class CardSetWithWordCount { public int Id...
Jon Skeet
people
quotationmark

This is a slightly more efficient version of felix-b's answer - it doesn't require creating a new list. It will return as soon as it's sure of the result, without any need for checking the rest of the elements. string... more 1/27/2018 9:28:29 AM

people