Browsing 7239 questions and answers with Jon Skeet

Set currency format using Culture and Globalization

I want to format currency at global level. For English currency format will display as $XXX,XXX.XX. If French then currency format will display as XXX.XXX,XX$. How would I set...
Jon Skeet
people
quotationmark

Use the NumberFormatInfo via CultureInfo.NumberFormat and there are various currency-related properties, including CurrencyDecimalSeparator and CurrencyPositivePattern. It looks like you want a positive pattern of 1 - you'll have to look... more 12/6/2013 2:49:50 PM

people

Error in return statement in generic methods

My problem is with the return statement in each method,the error in netbeans says: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad...
Jon Skeet
people
quotationmark

The compiler doesn't know that you can multiply and add T values - it's not the return part which the problem, it's the expression itself. You'll see the same effect if you split the two parts: T result = a + b; return result; It will... more 12/6/2013 1:53:17 PM

people

NPException in ArrayList? Java

So I keep getting the NPException when running my spell checker. The exception occurs in my "edits" method. So here is the code of the method: public ArrayList<String>...
Jon Skeet
people
quotationmark

The error occurs at the 4th line, "result.add(word.substring(1));" Then word must be null. result clearly isn't. You should look at what's calling this code, and work out why it's passing in a null reference. You might also want to... more 12/6/2013 7:10:24 AM

people

ASP.NET MVC Get the latest date even if it has the same date

The code below shows the status of the recent date. It compares if the first date is recent than the second date, and so on. But I have a problem. What if the first date and the...
Jon Skeet
people
quotationmark

I found your question hard to follow, but I suspect the simplest approach is just to use else: if (...) { data.s = "For approval MM"; } else if (...) { data.s = "For approval MP"; } else if (...) { data.s = "For approval... more 12/6/2013 7:04:54 AM

people

Removing a String from an ArrayList

So I have a problem that takes the names of people from a user and stores them in an ArrayList(personalNames). After that I need to take that list and remove any name that has...
Jon Skeet
people
quotationmark

If it's the "how do I remove an element from an ArrayList<>" part which is causing problems, and you want to check all the values, you probably want to use an Iterator and call remove on that: for (Iterator<String> iterator =... more 12/6/2013 6:49:44 AM

people

Random.Next() always returns 0

I am using a single Random instance to rapidly get random numbers in a Parallel query, but I have noticed that, eventually, Random.Next always returns zero. Is there a reason for...
Jon Skeet
people
quotationmark

Random isn't thread-safe. You should use a different instance of Random for each thread, instead. I wouldn't suggest locking as you've suggested, as otherwise if that's a significant part of your overall time, it could end up being no... more 12/5/2013 10:31:08 PM

people

ToString call on dynamic type variable behaves differently in C#

Can someone explain to me the difference between these two string variables : dynamic dateStrAsDynamic = "12/10/2013"; var dateStrFromDynamic =...
Jon Skeet
people
quotationmark

I mean "which dynamic operation?" The one that invokes a method using a dynamic variable as the argument. Note that the type dateStrFromDynamic will still be dynamic - the compiler doesn't know that ToString() will definitely return... more 12/5/2013 7:54:05 PM

people

Why converting int from SqlDataReader to long results in InvalidCastException?

I get an Invalid Cast Exception on the following line: DestMinSeq = (long)rdr["MinSeq"]; When I change the query to cast MinSeq to BIGINT instead of INT, it works. Question:...
Jon Skeet
people
quotationmark

Question: why should it be illegal to cast a short to a long? You're trying to cast a boxed short to a long. You can see this without a database: int x = 10; object o = x; long y = (long) o; // Bang! If you cast to the right type... more 12/5/2013 5:21:46 PM

people

Unparseable date: "2011 12 08T02:01:02+01:00"

Following code throws the exception when try to parse from the string (2011-12-08T02:01:02+01:00): image.setLastUpdated(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz") ...
Jon Skeet
people
quotationmark

You need X as the specifier for the UTC offset as it's in ISO-8601 format, as per the SimpleDateFormat documentation. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX") more 12/5/2013 2:42:58 PM

people

Why do I get 'System.UriFormatException: Invalid URI: Invalid port specified.' when using an IPv6 URI?

Why does this var uri = new Uri("ftp://1111:2222:3333::43/testing/1kb.zip"); Throw this exception? System.UriFormatException: Invalid URI: Invalid port specified. at...
Jon Skeet
people
quotationmark

From RFC 2732: To use a literal IPv6 address in a URL, the literal address should be enclosed in "[" and "]" characters. For example, this works fine: var uri = new Uri("ftp://[1111:2222:3333::43]/testing/1kb.zip"); If you... more 12/5/2013 2:37:48 PM

people