Browsing 7239 questions and answers with Jon Skeet

C#/.NET: XDocument always has the wrong encoding

I've got a problem with the XDocument class. I am loading information of online XML API, for my first WP8.1 App, like this: try { var xmlDoc =...
Jon Skeet
people
quotationmark

Rather than read the data into a byte array and decoding it yourself, just read it as a stream and let XDocument.Load detect the encoding from the data: using (HttpClient http = new HttpClient()) { using (var response = await... more 1/31/2016 10:20:18 PM

people

How to generate and display a sequence (loan exercise)

I would like to create a sequence, where: a0 = 1100000 a1 = 1100000 * 1.012 - 25000 a2 = (1100000 * 1.012 - 25000) * 1.012 - 25000 a3 = (1100000 * 1.012 - 25000) * 1.012 - 25000)...
Jon Skeet
people
quotationmark

You're declaring a new a0 variable in each iteration of the loop, and always initializing it with the value 1100000. If you want to use the value from the previous iteration within the loop, you need to declare it outside the loop: int a0... more 1/31/2016 9:19:31 AM

people

Declaring literal dates in lambda expression

I'm currently developping an simple engine which generates lamda expressions from different classes (using reflexion and expression factories). I have an issue trying to make...
Jon Skeet
people
quotationmark

Assuming you really mean you want to generate an appropriate expression tree with a ConstantExpression in, I think you'll have trouble doing that with an actual lambda expression. However, you could construct the relevant expression tree... more 1/29/2016 5:15:43 PM

people

Keeping head reference while traversing Linked Lists?

I'm revising Linked Lists and in the book I'm using they suggest the following code to search for a specific value: public ListElement<Integer> find(...
Jon Skeet
people
quotationmark

You can - but then it would be a somewhat misleading piece of code. If I look at a variable called head, I'd expect it to be the head of a list - whereas if I do: head = head.next(); ... then head refers to something which isn't the... more 1/29/2016 2:05:20 PM

people

Creating a list with elements count dependable from the another list

Title may not be as accurate as I'd want it to. I'll show it to you using an example: List<int> anotherList = new List<int>(); [0] 3 [1] 6 [2] 2 [3] 3 [4] 2 [5] 2 [6]...
Jon Skeet
people
quotationmark

LINQ makes this easy with SelectMany and Enumerable.Repeat: var result = input.SelectMany(x => Enumerable.Repeat(x, x)).ToList(); The SelectMany is a flattening operation: for each input in the original sequence, generate a new... more 1/29/2016 11:07:08 AM

people

what's going wrong in my below writing and reading in ObjectStream

below code to write my objects and byte[] into file with sigBytes being my byte[] ObjectOutputStream outputOS = new ObjectOutputStream(new FileOutputStream(outputFile)); ...
Jon Skeet
people
quotationmark

I believe I understand what may be going wrong here... you're currently using read(sigBytes) which does not guarantee that it will read all the data you've requested. In general, InputStream.read(byte[]) and InputStream.read(byte[], int,... more 1/29/2016 6:50:52 AM

people

How do I optimize an algorithm to decompose a number as the sum of two squares?

I have a simple math algo. All it does is it takes an input and finds i,j such that i^2 + j^2 = input with the restriction that j = i (so that it doesn't print it's counterpart...
Jon Skeet
people
quotationmark

You've got an O(n2) algorithm for no obvious reason. It's easy to make this O(n1/2)... Loop from 1 to the square root of n/2 (for variable i) - because when i is greater than sqrt(n/2) then i*i + j*j will be greater than n for any j... more 1/28/2016 10:07:53 PM

people

Why am I not allowed to use the character 𝄞 in a Java source code file as a variable name?

What are the rules for the characters that can be used in Java variable names? I have this sample code: public class Main { public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

What are the rules for the characters that can be used in Java variable names? The rules are in the JLS for identifiers, in section 3.8. You're interested in Character.isJavaIdentifierPart: A character may be part of a Java... more 1/28/2016 7:32:22 PM

people

Which one is more efficient of using array list?

Which one is more efficient to instantiate a list ? List<Type> list = new ArrayList<Type>(2); list.add(new Type("one")); list.add(new...
Jon Skeet
people
quotationmark

They create different types of objects. new ArrayList<>() creates a java.util.ArrayList, which can be added to, etc. Arrays.asList() uses a type which happens to also be called ArrayList, but is a nested type... more 1/28/2016 2:50:29 PM

people

Getting "java.nio.charset.UnmappableCharacterException: Input length = 1"

I am getting UnmappableCharacterException on the collect() method call (or on the toList() call): private static void handleTransaction(Path a_filePath, String...
Jon Skeet
people
quotationmark

The files I read are csv files which were created on solaris. I run the jar on Windows 2012 server Well that's probably the problem then. You're using the platform-default encoding for both reading and writing the file. If the files... more 1/28/2016 11:23:05 AM

people