Browsing 7239 questions and answers with Jon Skeet

Stringindexoutofboundsexception in android

I am trying to create a calculator app.IN order to avoid duplication of operators i am checking if the previous character in the string is an operator.But i am getting...
Jon Skeet
people
quotationmark

This line is broken for any string with a length other than 1: value = result.substring(result.length()-1).charAt(result.length()-1); Consider different lengths: 0 (result is an empty string): result.substring(result.length() - 1)... more 5/15/2014 11:11:04 AM

people

Converting a set of characters into a different string array using java

I am trying to read a data from a file. I have following code. public void ReadFile() { File sdcard = android.os.Environment.getExternalStorageDirectory(); File...
Jon Skeet
people
quotationmark

You're currently reading a byte at a time, because you're using InputStream. That's the first thing to fix - you should be using a Reader for text data. The best approach is to wrap your InputStream in an InputStreamReader. Next, it... more 5/15/2014 10:08:14 AM

people

Appending text files using buffered type writer java

I'm trying to store data that is being inputted by a user and store it in a text file to be manipulated later on, I'm having problems with it keep on overwriting the first lines...
Jon Skeet
people
quotationmark

The BufferedWriter isn't the problem - it's the way you're constructing the FileWriter. You can simply use the constructor overload which takes an append parameter: new FileWriter("./Cat Info.txt", true) Personally I would avoid using... more 5/15/2014 9:31:21 AM

people

What is the equivalent of DateTime.FromOADate() in Java (double to Datetime in Java)

C# has a DateTime.FromOADate() method. What is the equivalent of DateTime.FromOADate() in Java ? This is my C# code : var b = new byte[8]; b[0] = 0x20; b[1] = 0x64; b[2] =...
Jon Skeet
people
quotationmark

This looks like it works... basically ToBinary just returns a representation where the bottom 58 bits are the ticks since the BCL epoch in UTC. This code just reverses that private static final long UNIX_EPOCH = 62135596800000L; public... more 5/15/2014 6:25:13 AM

people

How to fetch data from List<dynamic>()?

My code is like this var eventDocs = new List<dynamic>(); foreach (var node in eventtypeNode.GetDescendantNodes()) { string files =...
Jon Skeet
people
quotationmark

You won't get anything from Intellisense precisely because you've got a List<dynamic>. You're saying, "I don't know at compile-time what this list will contain. When I access members of the elements, just bind that dynamically at... more 5/15/2014 6:09:13 AM

people

What does it mean when an async method uses time on a thread?

From MSDN: The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its...
Jon Skeet
people
quotationmark

My takeaway from the "async and await keywords don't cause additional threads to be created" is that it simply uses threads already created from the thread pool No, that's not true at all. async and await don't use the thread pool at... more 5/15/2014 5:55:24 AM

people

Declare Custom Attribute that create a new object

I'm not sure what i am trying to do is actually possible. I want to create a new Custom Attribute where by when the attribute is declared the user creates a new object. I'm...
Jon Skeet
people
quotationmark

The simplest approach would be to take several separate parameters, and create the Field instance based on those parameters. You can only configure attributes with compile-time constants, and new Field(...) isn't a compile-time... more 5/14/2014 4:33:19 PM

people

Making code unreachable

What is the best way to mark code unreachable, so the compiler won't give an error message? A short example: int x; if (y == 0) x = 1; else if (y == 1) x =...
Jon Skeet
people
quotationmark

I would go with throwing an exception - it indicates that the world is not in a state you expect, and you'd be better off quitting before you do any damage: if (y == 0) { x = 1; } else if (y == 1) { x = 2; } else { throw new... more 5/14/2014 4:20:20 PM

people

How is the "empty string" sequence represented under the hood in Java?

Throughout my career I've often seen calls like this: if( "".equals(foo) ) { //do stuff }; How is the empty string understood in terms of data in the lower-levels of Java? ...
Jon Skeet
people
quotationmark

There's no such thing as "the empty string character". A character is always a UTF-16 code unit, and there's no "empty" code unit. There's "an empty string" which is represented exactly the same way as any other string: A char[]... more 5/14/2014 3:07:48 PM

people

Resharper says to convert foreach to LINQ, but is it possible?

I have a Project entity that has a navigational property which is a list of Addresses. On a search page I allow the user to search for projects by several addresses at a time. Is...
Jon Skeet
people
quotationmark

I suspect the simplest approach is to create a List<string> for the street names you're searching for first - I would expect those to be convertible: var searchedStreets = searchedAddresses.Select(x => x.StreetName).ToList(); var... more 5/14/2014 1:51:20 PM

people