Browsing 7239 questions and answers with Jon Skeet

Variable in XPathSelectElement

I'm using XPathSelectElement to store data into an XML file. How can I in the code snippet below use a global variable name instead of id name 'City1'? I want the code to...
Jon Skeet
people
quotationmark

I'd just use the selection methods within LINQ to XML instead: XElement fname = country.Element("country") .Elements("city") .Elements("major") .Where(x =>... more 10/31/2013 7:23:50 AM

people

In Java when is the final block in a constructor executed?

For example: /** * Constructor */ public Test(InputStream in){ try{ this.inputStream = in; } finally{ inputStream.close(); } } Is the InputStream that is...
Jon Skeet
people
quotationmark

It's executed as part of a constructor. The code executed within a constructor is just normal code - there's no odd control flow there. (I mean after the call to the superclass constructor, and the variable/instance initializers being... more 10/31/2013 7:12:48 AM

people

Reading XML to get value of a tag c#

I have my XML as <?xml version="1.0" encoding="utf-8" ?> <configuration> <recordsDirectory>F:/model_RCCMREC/</recordsDirectory> ...
Jon Skeet
people
quotationmark

Yes, SelectSingleNode("recordsDirectory") will return null, because you're applying that XPath to the document itself - which doesn't have a recordsDirectory element at the top level, it has a configuration element. You... more 10/31/2013 7:08:11 AM

people

Java seems to not recognise forward slash

I am trying to count the number of forward slashes in a URL. However Java and StringUtils don't seem to want to do this. I suspected it was related to encoding but using...
Jon Skeet
people
quotationmark

This is the problem: URLDecoder.decode("url", "UTF-8") That should be: URLDecoder.decode(url, "UTF-8") Otherwise you're decoding the literal "url" - which will decode to itself ("url") - and the string "url" doesn't contain any... more 10/31/2013 6:59:04 AM

people

How do I run a thread in java that lets me know when another thread has died?

Assume that I have a thread A in java. This thread continues to perform some task A. I have another thread B that must perform a task B only after Task A is finished. How do I...
Jon Skeet
people
quotationmark

You can use Thread.join() to basically block one thread until another thread terminates: // In thread B threadA.join(); doStuff(); Note that this won't work properly if you use a thread pool e.g. via an executor service. If you need to... more 10/31/2013 6:45:52 AM

people

Is this code threadsafe? Static method modifying http servlet request

I've a class with a static method which takes two parameters- HttpServletRequest request and HttpServletResponse response : public class RequestProcessor { public static...
Jon Skeet
people
quotationmark

Yes, that should be absolutely fine, so long as the method isn't using/modifying any shared mutable state, and you only call it within normal servlet processing - so each thread that calls it will be passing in the request/response that... more 10/30/2013 9:04:51 PM

people

How do I reverse through a linked list

I need to be able to go back through this linked list, however no matter what I try it doesn't work. Can you point me in the right direction? using System; using...
Jon Skeet
people
quotationmark

Your linked list is only singly linked - each item contains a reference to the next item, but not the previous one. You can't walk backwards through a list like that. (Not without effectively building another collection with all of the... more 10/30/2013 9:01:40 PM

people

Working with various Calendar TimeZone in Java (without using Joda Time)

I was looking for a way to get current time in various timezones based on an user input. I know I could use Joda Time! but is that the only way? Isn't there an option in Java...
Jon Skeet
people
quotationmark

Yes, that would show the same value in every case (or milliseconds apart) because the three calendars all refer to the same instant in time (execution time notwithstanding) and that's all that a java.util.Date represents. That's the result... more 10/30/2013 9:00:04 PM

people

Namespace accessors Vs. "Using" accessors

Theoretical question: is it any difference between doing this: using System; ... var foo = new String("foo"); and this: var foo = new System.String("foo"); DLL loading? A...
Jon Skeet
people
quotationmark

No, they'll be compiled to absolutely identical IL. The using directive (there's no such term as "namespace accessor") is just a way of telling the C# compiler that it should look in that namespace when trying to resolve simple names to... more 10/30/2013 5:49:58 PM

people

Why do I always get the last elements of my string array?

I am trying to read all strings from a text file and save them in an array of strings. However, when I try to print the contents of my string array only the last part is printed....
Jon Skeet
people
quotationmark

Look at this loop: while(fgets(buffer, BUFFERSIZE, stdin) != NULL){ text[i] = buffer; i++; } You're setting every element of your array to the exact same value: the value of buffer, which is a pointer to a piece of memory. On... more 10/30/2013 5:45:44 PM

people