Browsing 7239 questions and answers with Jon Skeet

Storing Byte arrays and strings in the same file

I am making a program which requires me to store login information for users. I already have the code set up which creates a hashed Byte[] array of their passwords and salts them...
Jon Skeet
people
quotationmark

The simplest option would probably be to just convert the byte arrays to text using base64 or hex (not calling new String(bytes)) and then just use a property file or any other text storage scheme you fancy. You could go the other way,... more 2/13/2015 2:14:32 PM

people

How to check membership of an event handler from outside the owning class?

This question asks if there is a way to find if the code has already added its own event handler to an event. However, the answers given only work from inside the same class that...
Jon Skeet
people
quotationmark

That's impossible. Basically, the only operations you have with an event from the outside are "subscribe" and "unsubscribe". Now you could always unsubscribe before you subscribe. Unsubscribing is a no-op if the specified handler isn't... more 2/13/2015 1:57:45 PM

people

try with resources closing the resources unexpectedly

I am trying to invoke the following method inside a while loop. First time, it invokes fine but on 2nd loop execution, it throws an IOException public String...
Jon Skeet
people
quotationmark

I suspect you're calling getInputString more than once. The first time, it should work fine - but then you're closing System.in (by closing the BufferedReader wrapping InputStreamReader wrapping System.in)... which means the next time you... more 2/13/2015 12:05:08 PM

people

Calculate the annual use of a service starting from the date of signing

I need to calculate the annual use of a service starting from the date of signing. Something like: select Count(*) from TABLENAME where Date >= MYDATE MYDATE need to be...
Jon Skeet
people
quotationmark

Well, I'd do it in Noda Time, myself: LocalDate subscriptionDate = ...; LocalDate today = ...; // Need to take time zone into account int years = Period.Between(subscriptionDate, today); return subscription.PlusYears(years); With .NET... more 2/12/2015 5:00:23 PM

people

Remove node from XDocument

I have a xml config file that I am trying to remove a node from. My xml document is as follows <appSettings> <add key="value1" value="27348614" /> <add...
Jon Skeet
people
quotationmark

This is the problem: AppStoreXML.Root.Descendants("appSettings") You're trying to find descendant elements of appSettings which are also called appSettings and have the specified attributes. You want the add elements... more 2/12/2015 4:54:49 PM

people

How struct variables are stored in C#

I Read this StackOverflow Question And i read this blog here I couldn't understand completely. Here are my doubts. struct S { private int x; private int y; public...
Jon Skeet
people
quotationmark

Because it is initialized again in the second line , and called the constructor with (3,4). You've called the constructor, but the constructor itself hasn't finished - so the assignment to s never takes place. This of this: s = new... more 2/12/2015 2:57:46 PM

people

What happens to "System.out.println()" in executable jar?

Suppose I've created an executable jar from a code where I have used System.out.println() When we run the executable jar, there is no console. So, what happens to this line?...
Jon Skeet
people
quotationmark

There's nothing special about running code in an executable jar file. If you run it from a console, e.g. with java -jar foo.jar the output will still go to the console. If you run the code in some way that doesn't attach a console - such... more 2/12/2015 12:36:04 PM

people

How to get Expression for Nullable values ( fields ) without converting from Expression.Convert in C#?

I am dealing with the scanario of Nullable types during formation of dynamic query expressions. These expressions would fetch filtered data from any SQL Tables( interfacing with...
Jon Skeet
people
quotationmark

Here's a short but complete example showing how to build the c => c.Weight.HasValue && c.Weight.Value != 5000f expression tree. I've removed a lot of irrelevant code from the question: using System; using... more 2/12/2015 11:57:16 AM

people

How to validate with ComparisonChain?

I'm using com.google.common.collect.ComparatorChain to implement a custom comparator for different objects. Now, the following tests fails, but why? @Test public void...
Jon Skeet
people
quotationmark

You're ignoring the return value of comparator.compare("a", "b"). Calling compare() doesn't change the state of the existing comparison - it returns a comparison with the right state (which may or may not be the same - as long as the... more 2/12/2015 10:47:27 AM

people

How to print type of primitive variables in Java

I want know that the type of variable in my class for example: int x=1; char c=5; System.out.println(x+c);// out put is 6 but i want to know the type of x+c, whether...
Jon Skeet
people
quotationmark

Is there any method to find the type? Well, from a research point of view you could read the Java Language Specification. In particular, section 15.18.2 explains how the + operator works. If you want to do this just from an... more 2/12/2015 10:38:27 AM

people