Browsing 7239 questions and answers with Jon Skeet

Importing Java packages and classes

I'm talking about the stuff we import. Suppose, there is something like: import java.util.Scanner; util is the package and Scanner is the class, right? Can this be always...
Jon Skeet
people
quotationmark

Can this be always generalised and be said that in the import statement, the first one would be java/javax (is there any other?), the second one package, the third class and the fourth, if any will always be a method belonging to the... more 3/25/2015 6:12:54 PM

people

Unexpected output with array of Strings

I made an array of Strings with following code public class Main { static String[] words = {"watch", "on", "youtube",":","Mickey","en","de","stomende","drol"}; public...
Jon Skeet
people
quotationmark

But the actual output was [...] Not with the code you posted. The code you posted wouldn't compile, because: You didn't end the field initialization with a semi-colon If you had, you'd be trying to access an instance field without... more 3/25/2015 4:10:47 PM

people

Printing Octal characters in java using escape sequences

Please explain the below code public class Example{ public static void main(String[] args) { int i[]={9}; System.out.println("\700"); } } Please don't say...
Jon Skeet
people
quotationmark

Basically, you've got two characters there: '\70' and '0'. The escape sequence for octals is documented in the JLS as: OctalEscape: \ OctalDigit \ OctalDigit OctalDigit \ ZeroToThree OctalDigit OctalDigit The last of these doesn't... more 3/25/2015 2:33:58 PM

people

JodaTime unable to normalize PeriodType

So I have 2 date pickers for a start and end time, which give back a DateTime. I need to calculate the duration between these two times in the format of "hours hr mins hr", so...
Jon Skeet
people
quotationmark

I suspect the simplest way to do this is just to pass the PeriodType into the constructor instead: new Period( startDate.toLocalDateTime(), endDate.toLocalDateTime(), PeriodType.time()); Then you don't need to perform any... more 3/25/2015 12:03:59 PM

people

Unexpected behaviour with Java timer task

My Timer task is not functioning as it's supposed to. I have scheduled it to repeat a specific task every 3 seconds but this is not happening. As per Java documentations: ...
Jon Skeet
people
quotationmark

It works for me - but I suspect the problem is that you're letting the Timer get garbage collected: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task... more 3/25/2015 11:51:47 AM

people

Best Way to check if a java.util.Date is older than 30 days compared to current moment in time?

Here's what I want to do: Date currentDate = new Date(); Date eventStartDate = event.getStartDate(); How to check if eventStartDate is more than 30 days older than...
Jon Skeet
people
quotationmark

Okay, assuming you really want it to be "30 days" in the default time zone, I would use something like: // Implicitly uses system time zone and system clock ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime thirtyDaysAgo =... more 3/25/2015 11:08:33 AM

people

Allow parameter to have more than one value? for example value 1|| value 2 (C#,Light switch)

in my project "Light switch C#", I have a button which take me to a certain screen. The screen take one parameter of type string, for example "Office-Italy","Office- Germany" My...
Jon Skeet
people
quotationmark

No, a parameter can't have more than one value. So you'll need to change your method to accommodate what you need, e.g. by changing the parameter so that it's a collection of strings instead of a single string. You could change the... more 3/25/2015 10:32:13 AM

people

REST does not return alternate chars. Some chars are missing

I am trying to take a string as input from rest client and return json with alternate chars. However, some chars are missing from the string when it returns. On console, all the...
Jon Skeet
people
quotationmark

You're not only skipping alternate characters - you're also skipping characters whose UTF-16 code unit isn't even: if (c[i] % 2 == 0) That rules out 'o' (U+006F) twice, which is why you're getting "Hll" instead of "Hlool". It's not... more 3/25/2015 9:49:07 AM

people

Is this wrapper around AesManaged ok?

I need to encrypt/decrypt some strings. I've build my wrapper class according to the msdn documentation but with some changes. Since I want to encrypt/decrypt data with a given...
Jon Skeet
people
quotationmark

No, this is not okay. 1) You're using Encoding.Default in various places. Don't do that - it means you're at the whim of the platform you're on. Always use an explicit encoding, ideally UTF-8 in most cases. 2) You're using... more 3/25/2015 9:28:01 AM

people

Why an enum in an object instance has a static context?

I have the following class: public class HandleResourceReferencesParams { public Factory Factory { get; set; } public DataObject Resource { get; set; } public...
Jon Skeet
people
quotationmark

The error message is unfortunate, but it's not unfortunate that you can't do it... you're trying to access a member of a type, rather than a member of an instance of the type, but you're doing so "via" an instance. Basically, it's the... more 3/25/2015 9:06:52 AM

people