Browsing 7239 questions and answers with Jon Skeet

Factory method of IntBuffer (and other primitive type buffers) return instance but those classes are actually defined abstract

abstract class IntBuffer seems like you can't create an instance of this class anyway , because its declared abstract but at the same time , there's a static factory method...
Jon Skeet
people
quotationmark

Just because there's a static method there doesn't mean it's creating an instance of just IntBuffer. Here's a short example of the kind of thing a factory method can do: abstract class Abstract { static Abstract createInstance(int... more 3/2/2015 2:00:58 PM

people

Java FileWriter lost special characters

I've this code //write a file in a specific directory public static void writeFile(String comment, String outputFileName) { FileWriter fWriter = null; BufferedWriter...
Jon Skeet
people
quotationmark

FileWriter uses the platform-default encoding. It's very rarely a good idea to use that class, IMO. I would suggest you use an OutputStreamWriter specifying the encoding you want - where UTF-8 is almost always a good choice, when you have... more 3/2/2015 10:51:17 AM

people

Find single integer using Hashmap

A coding question: Given an array of integers, every element appears twice except for one. Find that single one. It requires a linear runtime complexity. My solution: public...
Jon Skeet
people
quotationmark

I suspect the time limit has been set with a specific implementation in mind. Your solution looks like it should be linear time to me - but it's probably a significantly larger constant factor than this code: public int... more 3/2/2015 6:58:21 AM

people

In Codename One, why can I not get FileInputStream to import or compile?

Here are my imports: import com.codename1.ui.*; import com.codename1.ui.util.*; import com.codename1.ui.plaf.*; import com.codename1.ui.events.*; import...
Jon Skeet
people
quotationmark

It looks like Codename One has omitted that class - and others, I suspect. Judging by the documentation, I suspect you want to use the com.codename1.io.FileSystemStorage class and its openInputStream method. You may well want to watch... more 3/2/2015 6:51:03 AM

people

Trick in C# with lambda and event

I'm trying to figure out if there is a way to accomplish something like this: Button button = new Button() { OnClick += (sender, e) => MessageBox.Show("hello") }; But it...
Jon Skeet
people
quotationmark

No, object initializers don't allow you to attach event handlers, unfortunately. The C# team is aware of this limitation - there was some hope that this would be in C# 6, but the feature was dropped. I hope it was dropped due to a lack of... more 3/1/2015 11:13:08 PM

people

Copying a file from a location to another location

I am trying to read a file and write it to a specific folder. I am using this code: private void saveFile(File file){ try { OutputStream out = new...
Jon Skeet
people
quotationmark

You're not closing the output stream. Therefore any data buffered within it before being written out to disk will be lost. You could use a try-with-resources statement to close the stream automatically - or you could just use: //... more 3/1/2015 10:06:49 PM

people

Difference between compiler on windows and ideone.com

I create two programs and each program has the same task. I create a stack with maximum 10 elements. When You write '+' it means that next, you will insert some number into stack...
Jon Skeet
people
quotationmark

Well this is the problem: for (; ; ) { char sign = char.Parse(Console.ReadLine()); ... } That's going to loop forever - there's nothing in the code to break out of the loop cleanly. However, Console.ReadLine() will return null... more 3/1/2015 5:42:53 PM

people

Nested foreach loops to get info from nested lists using C#

An object EmployeeInfoDocument contains a list of groups. Each group holds a list of employees. I am trying to use a nested foreach loop to write the values out to an XML file....
Jon Skeet
people
quotationmark

So this is your problem: foreach (Group group in ...) { ... foreach (Employee employee in group) { } } The compiler can't use foreach over group because it has no idea what you'd iterate over. You either need to... more 3/1/2015 5:31:37 PM

people

Get full stack trace with line numbers

Is it possible to get a full StackTrace object WITH line numbers at any given point in the code I found this: var stackTrace = new StackTrace(); That gives me the full...
Jon Skeet
people
quotationmark

I suspect you just want to call the overload taking a bool: var stackTrace = new StackTrace(true); From the documentation: Parameters fNeedFileInfo Type: System.Boolean true to capture the file name, line number, and column... more 2/27/2015 10:05:09 PM

people

What is the C# Syntax for something like date.IsWithIn(x months).Of(Comparison Date)?

The title is a bit whacky, but that's the question. I'm using C#. I'm trying to come up with several DateTime extension methods. While thinking about it, I wondered what syntax it...
Jon Skeet
people
quotationmark

IsWithin would have to return some sort of type representing a range of values, remembering the "centre" and the range size. Now Of could be an extension method of that, or could easily be a normal instance method, given that you'd be... more 2/27/2015 9:55:09 PM

people