Browsing 7239 questions and answers with Jon Skeet

Calling a property setter when changing a (nested) property inside the property object

insane title . . I know . . but bear with me . . consider the following case: a.ClassProperty.ValueProperty = 4; where: class A { [...] public PropertyClass...
Jon Skeet
people
quotationmark

Is there any way to change the getters and setters around to accomplish it. No, not really. The code: a.ClassProperty.ValueProperty = 4; is simply translated into: var tmp = a.ClassProperty; tmp.ValueProperty = 4; It's never... more 5/12/2014 9:18:20 AM

people

Why can't KeyAdapter understand shift +1 =?

To a computer: int key = e.getKeyCode(); if(e.getKeyCode() == VK_Shift && e.getKeyCode() == VK_1){ text = "!"; } Does nothing, but to me it should set it to an...
Jon Skeet
people
quotationmark

Why won't this work? You're asking whether a single value (e.getKeyCode()) is equal to both VK_Shift and VK_1. Unless those two have the same value (which they don't) the if condition can never be satisfied. The documentation makes... more 5/12/2014 9:10:20 AM

people

Fetch records with highest count from each group uniquely in Linq

I want only the records with max count from each group. The result must be grouped according to the constituency having maximum record count Here is the code. protected void...
Jon Skeet
people
quotationmark

It seems to me that you should aren't currently grouping by constituency at all. The query below should get you started. var query1 = from vote in db.Votes group vote by vote.CandidateID into g select new {... more 5/12/2014 6:24:47 AM

people

Compiling a Java file out of context

I have a large pre-compiled project with lots of packages and class files. I have extracted one of the class files and decompiled it and edited some of the code inside. Now I...
Jon Skeet
people
quotationmark

Just compile it with a classpath which refers to the existing class files (or the jar file that contains those class files). There should be no problem. However, note that if you change any constants in the file, those changes won't be... more 5/11/2014 6:04:21 PM

people

java.util.Vector cannot be cast to java.util.ArrayList

I am trying to return an ArrayList of database search results from a servlet to be displayed on a jsp page. Setting the arraylist as an attibute of request in the servlet and...
Jon Skeet
people
quotationmark

You haven't shown your displaySearchResults method, but it sounds like that returns a Vector rather than an ArrayList. But fundamentally, you shouldn't care which implementation it returns - if you just cast to List<Car> instead, you... more 5/11/2014 5:50:07 PM

people

Java get current time in Israel

I am tring to get the current time in Israel in java this is my code: TimeZone timeZone = TimeZone.getTimeZone("Israel"); Calendar calendar =...
Jon Skeet
people
quotationmark

You're setting the time zone in your calendar, but you should be setting it in your DateFormat. Additionally, you should be using Asia/Jerusalem as the time zone name. You don't need a Calendar at all - just new Date() gives the current... more 5/10/2014 7:33:15 PM

people

Assigning specific type object into an generic array

I have a generic type called Vector<T>, I created it as so, cause the T might be float or Complex : public class Vector<T> { #region Properties ...
Jon Skeet
people
quotationmark

It's not clear why you're converting to float? at all (or why you're using ulong as the index variable type...) but you just need to cast the result back to T - otherwise you can't assign it back into an array of type T[]. Additionally,... more 5/10/2014 7:23:44 PM

people

Overloading public methods

Are these 2 methods overloaded? private static int howManyChar (String s, char c, int index) { int count = 0; if (index >= s.length()) return 0; if...
Jon Skeet
people
quotationmark

Yes, they're overloaded. Within a class, accessibility is irrelevant to overloading. However, accessibility matters in that it's fine to have one private method in a base class with the same signature as another method in a derived... more 5/10/2014 6:47:26 PM

people

Why does a concatenation of integers and strings behave like this?

This code: public void main(String[] args) { String s1 = 50+40+"Hello"+50+50; System.out.println(s1); } Gives output of: 90Hello5050 Why?
Jon Skeet
people
quotationmark

It's just a matter of precedence and associativity. Your code is equivalent to: String s1 = (((50 + 40) + "Hello") + 50) + 50; So that's: String s1 = ((90 + "Hello") + 50) + 50; which is: String s1 = ("90Hello" + 50) + 50; which... more 5/10/2014 2:03:23 PM

people

IndexOutOfRangeException occured due to an if condition

An IndexOutOfRangeException just triggered in a little project I'm building, but why? Here's the boolean condition: (messages != null && messages.Rows[0] != null) What...
Jon Skeet
people
quotationmark

It sounds like you need to check whether or not Rows is empty too. We don't actually know the type of Rows, but you might want: if (messages != null && messages.Rows.Count > 0 && messages.Rows[0] != null) Or you could... more 5/10/2014 1:36:22 PM

people