Browsing 7239 questions and answers with Jon Skeet

Index out of Range exception in arrays

I am continuously getting out of range exception in my code. I went into the debug mode and found that it was giving an error for the zeroth index itself. Any help would be...
Jon Skeet
people
quotationmark

This line: public static string [] starGenerated = new string[numberOfForbiddenWords]; ... is executed when the class is initialized. That will happen while numberOfForbiddenWords is still 0, i.e. long before this line is... more 6/15/2015 9:30:37 AM

people

Why do two programs have forward referencing errors while the third does not?

The following does not compile, giving an 'illegal forward reference' message: class StaticInitialisation { static { System.out.println("Test string is: " +...
Jon Skeet
people
quotationmark

This is covered by section 8.3.3 of the JLS: Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope (§6.3). Specifically, it is a compile-time... more 6/15/2015 6:19:24 AM

people

Android get an error when i try to setRequestProperty to HttpURLConnection

i'm wrote simple download manager and i'm trying to set RESUME for all downloads. after googleing for how to do that. i know must be setRequestProperty for connection, but my code...
Jon Skeet
people
quotationmark

The problem is that you're calling connection.getContentLength() before you're calling setRequestProperty(). The content length is only available after you've made a request, at which point you can't set the request property... It's not... more 6/15/2015 6:00:04 AM

people

why does the compiler complain about missing ctor of WebSocketHandler?

I'm trying to use websocket in my project. to do so, I installed the package Microsoft Asp.Net SignalR, which consists of WebSocketHandler abstract class. i defined a class...
Jon Skeet
people
quotationmark

It seems wierd to me, because the definitioin of WebSocketHandler ctor gets a nullable value, which means the ctor could get no parameter No, it doesn't. There's a big difference between receiving a null value for a nullable type, and... more 6/14/2015 8:36:48 PM

people

Last two bits in a BitArray of length 4 are being ignored when converting to int

Consider the following BitArray: BitArray bitArray = new BitArray(new Boolean[] {true, true, false, false}); and in binary equals: 1100 Now I want to convert this to an int...
Jon Skeet
people
quotationmark

The constructor for BitArray(bool[]) accepts the values in index order - and then CopyTo uses them in the traditional significance (so bitArray[0] is the least significant bit) - so your true, true, false, false ends up meaning 0011 in... more 6/14/2015 8:21:29 PM

people

How to break if else if using C#

How do I break if-else-if.....Why its not working? its just checking all the conditions instead of performing the tasks. following is my code. I have checked it through...
Jon Skeet
people
quotationmark

I strongly suspect the problem is that the Text property is never null for any of txt_sel*. Assuming these are text boxes in a UI, it's much more likely that if there's no text in the text box, the Text property will return "" instead of... more 6/14/2015 6:37:04 AM

people

Why is 1_2_3_4 a valid integer literal in java?

Why does the following work? int a=1_2_3_4; System.out.println(a); // 1234
Jon Skeet
people
quotationmark

Numeric literals are specified in JLS 3.10.1. A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9... more 6/13/2015 7:00:36 AM

people

String to Double Conversion (Comma to dot issue)

Struggling with the basics - I'm trying to code a simple currency converter. The XML provided by external source uses comma as a decimal separator for exchange rate...
Jon Skeet
people
quotationmark

Just use decimal.Parse but specify a CultureInfo. There's nothing "random" about it - pick an appropriate CultureInfo, and then use that. For example: using System; using System.Globalization; class Test { static void Main() { ... more 6/12/2015 9:47:21 PM

people

Is it safe to use special characters in java code?

My language is Portuguese, so i need to write a method doSomething(), which in Portuguese is façaAlgo(). Java allows me to use special characters in the code, but is it safe? What...
Jon Skeet
people
quotationmark

It's valid Java, but you're slightly more likely to run into problems when: Transferring your source code between computers Compiling Editing Pretty much everything handles ASCII with no problems, and most encodings are either... more 6/12/2015 6:57:02 PM

people

Why can Toast be instantiated without the new keyword?

So for the Toast class, it can apparently be written like this : Toast toastMessage = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT) toastMessage.show(); Why exactly is it...
Jon Skeet
people
quotationmark

There's nothing special about Toast here. You're just calling a static method which creates an instance (or could possibly reuse an existing one - it's an implementation detail). This is a pattern you'll see all over the place -... more 6/12/2015 5:23:03 PM

people