Browsing 7239 questions and answers with Jon Skeet

Different result in RoundingMode.HALF_UP

Please comment me freely. What was wrong in the following program. It is giving different round result. public class Test { public static String round(double value, int...
Jon Skeet
people
quotationmark

You're calling the BigDecimal(double) constructor. That's documented as: Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal... more 11/24/2015 9:06:38 AM

people

Error on "instanceOf" when checking a subClass

I have a method, that accepts as parameter all classes that extend Persona.Class (Uomo.Class and Donna.Class extend Persona.Class). public PersonaDecorator(Class <? extends...
Jon Skeet
people
quotationmark

The instanceof operator tests whether a reference (the left operand) refers to an object which is an instance of the class named on the right operand. Here, persona will be a reference to an instance of Class, and an instance of Class can... more 11/24/2015 7:15:37 AM

people

Parsing string timestamp with time zone in 3 digit format followed by 'Z'

In the Hadoop infrastructure (Java-based) I am getting timestamps as string values in this format: 2015-10-01T04:22:38:208Z 2015-10-01T04:23:35:471Z 2015-10-01T04:24:33:422Z I...
Jon Skeet
people
quotationmark

I very strongly suspect the final three digits are nothing to do with time zones, but are instead milliseconds, and yes, the Z means UTC. It's a little odd that they're using : instead of . as the separator between seconds and... more 11/23/2015 5:55:10 PM

people

High accuracy time syncing between Android (Java) and .NET DateTime?

I need a high accuracy time conversion between a native Android app and a .NET-based time stamp. The .NET side simply uses: long timeNowTicks =...
Jon Skeet
people
quotationmark

Yes, I believe GregorianCalendar does indeed observe the Julian/Gregorian cutover - although I note that you're setting oldUtcTime to the 1st of February in 1AD, which is probably why you're seeing a difference of 20 days instead of 10... more 11/23/2015 5:26:34 PM

people

How to check if a field was initialized or contains the default value in Java

I'm mapping a json string to a class using jackson mapper. The class looks like this: class MyClass{ @JsonProperty("my_boolean") private boolean myBoolean; ...
Jon Skeet
people
quotationmark

You should check whether the field type is primitive, and if it is, check the value against a default wrapper. For example: Map<Class, Object> defaultValues = new HashMap<Class, Object>(); defaultValues.put(Integer.class,... more 11/23/2015 8:25:14 AM

people

keys() is undefined for the type JSONObject

Why do i get this error when i'm trying to get the keys of JSONObject ? its clearly in the API And I'm importing the correct library : import org.json.simple.JSONArray; import...
Jon Skeet
people
quotationmark

You're looking at the documentation for the wrong library. In json-simple, JSONObject extends HashMap, so you should use keySet()... or change to use the json.org library instead. more 11/21/2015 7:46:27 PM

people

Why won't this print any integers?

try { Scanner sc = new Scanner(new File("testing.txt")); while (sc.hasNextInt()){ int i = sc.nextInt(); //timing.add(i); System.out.println(i); ...
Jon Skeet
people
quotationmark

Your first value ("Michael") isn't an integer, therefore it never gets inside of the body of the loop. Perhaps you want to change the code to loop until it reaches the end of the file, reading and printing integers, but consuming (without... more 11/20/2015 3:54:07 PM

people

linq `from` clauses between generic and non generic collections?

I've these linq from clauses in a sentence: IEnumerable<T> collection = //...; IEnumerable<PropertyInfo> propertiesToFlat = typeof(T).GetProperties().Where(prop =>...
Jon Skeet
people
quotationmark

Basically, LINQ contains very few operations which work on non-generic collections. There's no Enumerable.Where(IEnumerable, ...) for example - only Enumerable.Where<T>(IEnumerable<T>, ...). The simplest way to fix this is to... more 11/20/2015 12:54:34 PM

people

In Java, why is instanceof a keyword and not a method?

In Java, why is instanceof a keyword and not a method? public static void main(String args[]) { Simple1 s = new Simple1(); System.out.println(s instanceof Simple); //...
Jon Skeet
people
quotationmark

Well, there is the method Class.isInstance... but basically it's a lower-level operation which has specific VM support, so it makes sense for it to be an operator. Aside from anything else, there's no real need to obtain the Class... more 11/20/2015 10:25:10 AM

people

Issue with "arithmetic operation resulted in overflow"

I have this expression long balance = (long)answer.Find(DppGlobals.TAG_TI_BALANCE).Get_QWORD(); which raises exception that there was overflow. The value on the right hand...
Jon Skeet
people
quotationmark

Given the comments, it sounds like you do just need to use unchecked arithmetic - but you should be concerned about the use of ulong in your API. If your aim is to just propagate 8 bytes of data, and interpret it as either an unsigned... more 11/20/2015 7:29:03 AM

people