Browsing 7239 questions and answers with Jon Skeet

Why does the `package` instruction in java make the class file impossible to run?

I'm trying to compile a java file with a package directive. However, when adding the directive to the most simple program stub, I get an error and cannot launch the program...
Jon Skeet
people
quotationmark

It's looking for the class de.train.Dummy1, which means it will look for a file called Dummy1.class in a directory de\train - but the file will actually be in the current directory. Options: Keep your source code where it is, but get... more 8/12/2015 10:51:32 AM

people

Roslyn failed to compile code

After I have migrated my project from VS2013 to VS2015 the project no longer builds. A compilation error occurs in the following LINQ statement: static void Main(string[]...
Jon Skeet
people
quotationmark

What does cause this issue? Looks like a compiler bug to me. At least, it did. Although the decimal.TryParse(v, out a) and decimal.TryParse(v, out b) expressions are evaluated dynamically, I expected the compiler to still understand... more 8/12/2015 9:42:54 AM

people

getting string from byte array in different OS with Java

I'm trying to convert a byte array in string with simple code: System.out.printf("%s \n", new String(b)); where b has this content (in hex chars): ...
Jon Skeet
people
quotationmark

Yes, that's because you're using the constructor that uses the platform default encoding to convert binary data to text. It's entirely reasonable for it to create different strings on different platforms - although I suspect your... more 8/12/2015 8:49:13 AM

people

Why i am getting a Number FormatException?

I made a program on the code chef.I am getting a correct output on my eclipse ide and when i submitted it the output shown on CodeChef ide is : Exception in thread "main"...
Jon Skeet
people
quotationmark

The immediate cause is that you're passing null into Integer.parseInt. Next you need to work out why that's happening... You're creating a new BufferedReader wrapping System.in on each iteration... that's likely to be reading more than... more 8/12/2015 8:01:34 AM

people

Handle exception when using Task.Run() in UI constructor

I have a constructor that call Task.Run() like this: public MyPage() { Task.Run(() => { MyHeavyCpuMethod(); }); } Here, MyPage() is the constructor of a UI...
Jon Skeet
people
quotationmark

One option is to use async/await... which doesn't work with a constructor, but which can work in a static method: public static async Task<MyPage> CreateInstance() { await Task.Run(...); // Anything else asynchronous you... more 8/12/2015 6:22:46 AM

people

getting type of element in a List in which List is the return type of method in java

I got every methods in my package using reflections and then based on the return type of every method, I want to do some operations. But unfortunately I had a problem with...
Jon Skeet
people
quotationmark

You can use getGenericReturnType which returns a Type rather than a Class, and allows you to get at all the type arguments etc. Short but complete example: import java.lang.reflect.*; import java.util.*; public class Test { public... more 8/12/2015 6:15:44 AM

people

Stream works without filter(predicate), gives "Incompatible types" with it

Why does the below code work only when the filter-predicate line is commented out. With the filter line, it gives me this error: Incompatible types: java.lang.Object cannot be...
Jon Skeet
people
quotationmark

You're using the raw form of Predicate<T> - using raw types is very often where you end up getting compile-time errors involving Object where you expected to be using a more specific type. All you need to do is change the... more 8/11/2015 4:59:22 PM

people

How to get "start of the year/month" in NodaTime?

I know there is DateTimeZone.AtStartOfDay method, but are there analogs to get start of the year/month? I know the easiest way is to hardcode those to "first day of X and 30...
Jon Skeet
people
quotationmark

You certainly don't need to go via DateTime - but to get the start of a year in a particular time zone, I'd just use: var startOfYear = zone.AtStartOfDay(new LocalDate(year, 1, 1)); Note that this will be useful to get the duration... more 8/11/2015 2:25:24 PM

people

Why Collectors.groupingBy gets confused with identity function?

I'm trying to count occurrences of integers in an array using Collectors.groupingBy from Java 8 API but I'm getting some strange compilation errors. Here is my...
Jon Skeet
people
quotationmark

counting() is declared as: static <T> Collector<T,?,Long> ... whereas you're trying to use it as if it produces an Integer. If you change your code to: Map<Integer, Long> x = l.stream().collect(groupingBy(i -> i,... more 8/11/2015 1:19:55 PM

people

Why is this not a statement

I am posting some data to a webservice here is the snippet: try { response = UniversalHttpUrlConnection.postJSONObject(ctx.getResources().getString(R.string.delete_one),...
Jon Skeet
people
quotationmark

What you've got is a local variable declaration - and that isn't allowed on its own as a statement... because it's pointless. Why declare a variable that you're not going to be able to use, because it's immediately out of scope? The... more 8/11/2015 10:48:26 AM

people