Browsing 7239 questions and answers with Jon Skeet

Scanner(System.in) infinite loop

Why I'm getting infinite loop in recursion method, without a chance to input any symbol to break it? class Test { int key=0; void meth(){ System.out.println("Enter the...
Jon Skeet
people
quotationmark

If nextInt() fails, it throws an exception but doesn't consume the invalid data. From the documentation: When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be... more 2/18/2015 4:04:33 PM

people

Java reflection getDeclaredMethods in declared order strange behaviour

This is a strange behavior happening with getDeclaredMethods ,here is the scenario, a class called Entity: public class Entity { private Object reference; /** * @return the...
Jon Skeet
people
quotationmark

How can I keep the declared order as it is in the class file, no matter what fields I set? You can't, with getDeclaredMethods. The documentation is very clear about this: The elements in the returned array are not sorted and are... more 2/18/2015 2:22:29 PM

people

Get list ofTwoLetterISOLanguageName from list of EnglishName

I have a list of languages in english form: German English French ... I want to get: DE EN FR how can I do this?
Jon Skeet
people
quotationmark

Sounds like you just need to take the complete list of neutral (language-only) cultures, join that with your English names list, and then project that: var languageCultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures); var... more 2/18/2015 1:26:46 PM

people

"Expression is always false" when casting with AS

I get the compiler warning Expression is always false on this code void Test(Part part) { var wire = part as Wire; if (wire == null) return; if (part == null)...
Jon Skeet
people
quotationmark

But if the cast to Wire fails and results in null does not mean part is null too. No, but the reverse is true - if part is null, then wire will definitely be null, so you'll already have returned... hence the warning. (I'm assuming... more 2/18/2015 10:41:49 AM

people

Java Multiplying 2 integers from strings

I've tried searching a number of threads in terms of multiplying 2 strings. However, it seems that most of the links I found weren't applicable or I couldn't interpret them well...
Jon Skeet
people
quotationmark

You can't multiply strings together. You can multiply numbers, and it sounds like that's what you want to do. Indeed, you're already parsing the strings as integers - and then ignoring the result. You just need to change this: ... more 2/18/2015 10:39:04 AM

people

C# one event with multiple subscribers issues with ordering

I have a class , lets call it class A . Class A has an event "OnSendData" . And there is another class named B . Class A has an arraylist with references of class B . Class A...
Jon Skeet
people
quotationmark

Where you're raising the event, you can call Delegate.GetInvocationList to get all the subscribers - and then use a separate task or thread per subscriber to call them all in parallel. Note that your subscribers will have to be... more 2/18/2015 10:02:00 AM

people

new keyword not forcing hiding OOP C#

I have the following scenario: I want to mock an interface so I can unit test my app. I am trying not to create a new mock class implementing my interface but creating a new...
Jon Skeet
people
quotationmark

Just using new creates a new method without associating it with the interface. You're using the interface in your real code (I assume) so you want the binding of the interface method to the new method. To do that, you have to declare that... more 2/18/2015 9:02:44 AM

people

Split java.util.Date collection by Days

Could you help me, please. I can't figure out the algorithm. I have a sorted collection of Dates, for example ArrayList like this: Wed Jan 22 00:00:00 MSK 2014 Wed Jan 22...
Jon Skeet
people
quotationmark

If you're happy with UTC days, life becomes simpler: The milliseconds-since-the-unix-epoch starts on a day boundary, so you can just divide Each day is 24 hours long, which is handy in many cases (e.g. if you're doing analytics with the... more 2/18/2015 8:19:55 AM

people

How to set data type of enum class?

I am creating an enum class in java, which is already created in .NET. This is .NET code. public enum Bezel : byte { Flashing, SolidOn, ...
Jon Skeet
people
quotationmark

Enums in Java and .NET are very different. In .NET, they're just named numbers, basically. In Java they're a fixed set of objects - which you can give extra behaviour etc. Also, the C# code isn't "extending" byte - it's just using byte as... more 2/18/2015 7:04:38 AM

people

XPathNavigator can't find nodes despite XmlNamespaceManager

I hate to ask yet another XPath/XmlNamespaceManager question, but I can't seem to figure this out. I used XpathVisualizer tool which can correctly detect the node using XPath...
Jon Skeet
people
quotationmark

You're specifiying the namespace manager - but not the namespace. The FuelGradeMovement element has inherited the "http://www.naxml.org/POSBO/Vocabulary/2003-10-16" namespace URI from its parent, so I believe you want: var iterator =... more 2/17/2015 3:56:06 PM

people