Browsing 7239 questions and answers with Jon Skeet

Difference between two LocalDateTIme objects in days

I need to find difference between current dateTime LocalDateTIme.now() and another LocaDateTime object called issueDateTime in number of days. Also days to be counted starting...
Jon Skeet
people
quotationmark

You can use ChronoUnit.DAYS for this. Compare the LocalTime values to see whether or not the start should be consider to be in "the next day": import java.time.*; import java.time.temporal.*; class Test { public static void... more 9/25/2014 11:32:52 AM

people

HashSet doesn't remove an object

Probably I miss something using a HashSet and HashCode, but I don“t know why this doesn't work as I thought. I have an object with the HashCode is overridden. I added the object...
Jon Skeet
people
quotationmark

Firstly, you're overriding GetHashCode but not overriding Equals. Don't do that. They should always be overridden at the same time, in a consistent manner. Next, you're right that changing an objects hash code will affect finding it in... more 9/25/2014 7:31:21 AM

people

Guessing a Random number with opportunities

So i have to make a program kind of game, is a guessing number game so I had made all the program but i want to tell to the user that you only he have 5 opportunities to guess the...
Jon Skeet
people
quotationmark

(Only giving hints - assuming this is homework, you should definitely work out the details for yourself.) Currently your loop structure is: do { ... } while (e != w); So you're going to loop until the player has guessed the... more 9/25/2014 6:03:19 AM

people

Convert List of names to Dictionary, where each entry corresponds to names that begin with a certain letter

I created some method that returns a Dictionary<string, List<Employee>> in this method i loop through a List<Employee> and look for the firstname and add this...
Jon Skeet
people
quotationmark

It sounds like you really want an ILookup<string, Employee> which is precisely a dictionary where each key maps to potentially multiple values. Yes, you can create a Dictionary<string, List<Employee>> but I would strongly... more 9/24/2014 10:19:23 PM

people

Cannot Parse Date and get Milliseconds

I am trying to parse a String into Date. I am using new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS").parse("20140923-14:32:34.456") My output is: (Date object) Tue Sep 23...
Jon Skeet
people
quotationmark

You're just printing out the result of calling Date.toString() which happens not to include the milliseconds. If you print out the result of calling getTime() on the date, you'll see that it ends in "456", showing that it has parsed the... more 9/24/2014 9:00:21 PM

people

Method overloading reference to is ambiguous

I'm trying to get better grips of function overloading. I've this test program (pardon me for my C inspired Java). public class Test { static void f(int x, double y) { ...
Jon Skeet
people
quotationmark

int is convertible to double in all of these languages, so if only one of them were available, it would be easy: convert whichever value needs to be converted to a double, and call the method. But in this case, both methods are "valid" in... more 9/24/2014 8:15:21 PM

people

How to use ArgumentOutOfRangeException with multiple params?

I have the following code. DoSomething(int min, int max) { if (min < 1 || min > 5) throw new ArgumentOutOfRangeException("min"); if (max < 1 || max >...
Jon Skeet
people
quotationmark

No, the argument for the ArgumentOutOfRangeException constructor should always be one of the parameter names. You can pick either of them - I usually assume that the earlier parameter is correct, so the later parameter is incorrect in... more 9/24/2014 8:09:01 PM

people

SimpleDateFormatter not working correctly

I have the following code snippet. @Override public String toString() { try { String calString = "Sat Sep 27 00:00:00 EDT 2014"; ...
Jon Skeet
people
quotationmark

You're using YYYY instead of yyyy. That means "week year", to be used in conjunction with "week of year". Just change YYYY to yyyy in both of your SimpleDateFormat constructors and you'll get output of: calString Sat Sep 27 00:00:00 EDT... more 9/24/2014 8:05:37 PM

people

HashMap different bucket index for a key possible?

My concern was to check how Java HashMap gets the same index for a key. Even when it's size expand from default 16 to much higher values as we keep adding Entries. I tried to...
Jon Skeet
people
quotationmark

You're missing the fact that every time the length changes, the entries are redistributed - they're put in the new buckets appropriately. That's why it takes a bit of time (O(N)) when the map expands - everything needs to be copied from... more 9/24/2014 7:50:36 PM

people

If the left operand to the ?? operator is not null, does the right operand get evaluated?

I'm looking at using the ?? operator (null-coalescing operator) in C#. But the documentation at MSDN is limited. My question: If the left-hand operand is not null, does the...
Jon Skeet
people
quotationmark

As ever, the C# specification is the best place to go for this sort of thing. From section 7.13 of the C# 5 specification (emphasis mine): A null coalescing expression of the form a ?? b requires a to be of a nullable type or... more 9/24/2014 7:01:30 PM

people