Browsing 7239 questions and answers with Jon Skeet

Java : How to create an array from object then store String and double in that array

I want to create an array from object then store in that array with 2 different type of date i'm still learning java and this is kinda my task and i searched many times and have...
Jon Skeet
people
quotationmark

You basically need to simplify your loop: on each iteration of the loop, ask for both the subject name and the grade, then create one object to store them both: for (int i = 0; i < gradeArray.length; i++) { ... more 11/28/2014 10:29:21 AM

people

Locales with ISO calendar only

I have a java program where the user can choose the locale which he wants from a checkbox. in order to populate this checkbox I use the Calendar.getAvailableLocales() method. The...
Jon Skeet
people
quotationmark

You can just check whether Calendar.getInstance(Locale) returns a GregorianCalendar: for (Locale locale : Calendar.getAvailableLocales()) { if (locale.getCountry().length() > 0 && Calendar.getInstance(locale) instanceof... more 11/28/2014 9:13:22 AM

people

Why specifying a generic argument as interface is not an error with a class constraint?

If I have a generic method like this one: static void Foo<T>() where T : class { } I could call it by specifying an interface type, for example...
Jon Skeet
people
quotationmark

The "class" constraint is really a "reference type" constraint - it doesn't specifically mean classes. (It works with delegate types too.) From the MSDN page on generic type constraints: The type argument must be a reference type; this... more 11/27/2014 11:45:20 PM

people

How to use the Java 8 Duration.of(amount, unit) method?

I tried this: Duration.of(3, SECONDS) as per the example, but what is the SECONDS constant coming from? It's not evaluating to anything.
Jon Skeet
people
quotationmark

It's a value from the ChronoUnit enum, implementing TemporalUnit - your code can be written as: Duration.of(3, ChronoUnit.SECONDS); I'm not sure what you mean by "it's not evaluating to anything" - it's specifying what units you want to... more 11/27/2014 11:42:53 PM

people

What is the class type of a superclass ref pointing to a subclass object?

I have the following codes: 1. public class Tester 2. { 3. public static void main(String[] args) 4. { 5. A a = new B(); 6. ...
Jon Skeet
people
quotationmark

a is not an object. It's a variable. The type of the variable is A. The type of the object that the value of the variable refers to at execution time is B. The compiler resolves everything against the compile-time type of the expressions... more 11/27/2014 6:45:20 PM

people

Celsius to Fahrenheit Conversion Form

I am trying to create a form that will convert Celsius to Fahrenheit and vice versa, but I am running into an error with the Conversion buttons code. The problem I am getting is...
Jon Skeet
people
quotationmark

My question is how can I reword the processing section to not cause this error. Well, there are two situations that the compiler is concerned about: If the input is invalid, you're already changing lblConvertedTemperature.Text... more 11/27/2014 5:47:14 PM

people

JodaTime throws IllegalArgumentException with Date string from EXIF metadata

I'm writing some code to store metadata from uploaded images in the database. One of those fields is the photo Capture Date. The API I'm using returns the Capture Date in the...
Jon Skeet
people
quotationmark

The Joda Time DateTime and LocalDate constructors which take Object have documentation including: The String formats are described by ISODateTimeFormat.dateTimeParser(). The text you're providing is not in an ISO format. Instead,... more 11/27/2014 3:42:55 PM

people

awt Frame constructor not accepting naming of GraphicsConfiguration class

I am just starting my Java endeavour and have just finished reading through the learning java sections on the oracle website. So I'm just looking through a few packages seeing...
Jon Skeet
people
quotationmark

When you call a method or constructor, you pass arguments - values - you're not declaring the parameters like you do when you declare the method or constructor. So it should be something like: GraphicsConfiguration gc = ...; // Whatever... more 11/27/2014 3:32:56 PM

people

Mock abstract protected method

I have an abstract class: public abstract class MyClass { protected abstract bool IsSavable(); protected bool IsExecutable() { //New mode or edit mode ...
Jon Skeet
people
quotationmark

I would like to unit test this class. Therefor I need to mock the "IsSavable" method. It should always return "true". No, that's a non-sequitur. You can just create a subclass which does what you want: // Within your test code class... more 11/27/2014 3:24:36 PM

people

converting string into json object

I am getting a string like String s = "abc:xyz". Is there any direct method to convert it into JsonObject having abc as key and xyz as value. I know there a way by converting...
Jon Skeet
people
quotationmark

It sounds like you just want: String[] bits = s.split(":"); if (bits.length() != 2) { // Throw an exception or whatever you want } JSONObject json = new JSONObject(); json.put(bits[0], bits[1]); more 11/27/2014 1:35:20 PM

people