Browsing 7239 questions and answers with Jon Skeet

What is the "general contract" of a method

I'm looking at the the java docs for DataInputStream here: http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html I'm looking to see what its methods do so I look...
Jon Skeet
people
quotationmark

It just means that the documentation of DataInput.readBoolean contains more detail. In particular, that documentation states: Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method is... more 1/3/2015 6:59:36 PM

people

How to throw an ArgumentNullException on a property

I have a method where it checks if a property is null or not. I know how to throw an arguement null exception if the object is null but how do you throw an arguementnullexception...
Jon Skeet
people
quotationmark

You shouldn't throw ArgumentNullException if the argument (recall) isn't null. Just ArgumentException is appropriate here when it's some problem with the argument other than it being null: if (recall == null) { throw new... more 1/2/2015 2:57:31 PM

people

output is displaying all the files , but in the end it displaying a string of question marks(?????????????).what to do

This code shows the content of multiple files using vector object, extracting from this object using enumeration. import java.util.*; import java.io.*; class IO14 { public...
Jon Skeet
people
quotationmark

You seem to be expecting that InputStream.read() will return 1 when it's reached the end of its content - it won't, it'll return -1. So your while loop should look like this: while((i=sis.read()) != -1) ... although you're still... more 1/2/2015 12:31:37 PM

people

JodaTime how to get current time in UTC

I want to get the current time in UTC. What I do so far is following (just for testing purposes): DateTime dt = new DateTime(); DateTimeZone tz =...
Jon Skeet
people
quotationmark

You're making it far more complicated than you need to: DateTime dt = new DateTime(DateTimeZone.UTC); No conversion required at all. If you find you actually need to convert, you can use withZone. I'd suggest you avoid going via... more 1/2/2015 11:24:13 AM

people

Why I have to write the namespace to access to this extension method?

I have a project that has class to implement extension methods for some type. For example I have this class for ObservableCollection: using System; using...
Jon Skeet
people
quotationmark

Two problems: You can't use extension methods as if they were static methods of the extended type System.Decimal already has a Parse method, and the compiler always looks for "real" methods before extension methods. In fact, you can... more 1/2/2015 9:33:29 AM

people

SimpleDateFormat("dd MMM YYYY") printing year one year ahead

I am using SimpleDateFormat("dd-MMM-YYYY") in my code, which is giving wrong output. SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-YYYY"); ...
Jon Skeet
people
quotationmark

You're using YYYY, which is the "ISO-8601 week year". That should almost always be used in conjunction with w, "week in year". You want yyyy to show the normal calendar year. The reason they're different is that the first ISO-8601 week of... more 1/2/2015 8:46:03 AM

people

How to do a culture invariant conversion from int to String in Java?

For converting an integer to string in C# (with invariant culture), we do something like below: int num = 123; string numAsString =...
Jon Skeet
people
quotationmark

or does Integer.ToString(int) inherently does culture invariant conversion? It does. The documentation isn't as clear as it might be, but it doesn't use any grouping separators, negative numbers are always prefixed with '-', and ASCII... more 1/2/2015 6:56:49 AM

people

Find Number of 0's at end of int

I wanted to find out the number of 0's at end of integer. Suppose anyone enter 2020 it should count 1,if number is 2000 it should show 3 etc; I tried following but doesn't...
Jon Skeet
people
quotationmark

You're not changing anything within your loop - so basically, on each iteration it will either increase Count or it won't, and it'll do the same thing each time - so Count will either be the length of the string, or it will be 0. The... more 1/2/2015 6:51:19 AM

people

How to use a system path to load xml file in XElement in windows phone

I am working with a windows phone8 application. I have a file stored at a particular location in device and i want to open it with XElement. If i give path to that file it says...
Jon Skeet
people
quotationmark

The first approach I'd suggest is opening the path as a stream, and then use XDocument.Load(Stream). (There's a similar method for XElement, but I typically use XDocument when I'm loading a whole document, to make it clear that that's what... more 1/2/2015 6:46:04 AM

people

Java Different MD5 output for same input?

My code to generate md5 looks like @Nonnull static String getAuthCode(@Nonnull final String email, @Nonnull final String memberExternalId, @Nonnull...
Jon Skeet
people
quotationmark

The problem pointed out in comments is a problem - you should define which encoding you want to use. I'd recommend using UTF-8, e.g. messageDigest.update(stringForHashCode.getBytes(StandardCharsets.UTF_8)); A bigger problem, however, is... more 1/1/2015 8:56:35 PM

people