Browsing 7239 questions and answers with Jon Skeet

Java convert unix timestamp to wrong time

I have unix timestammp stored in mysql. I am converting it into time. It displays wrong time. Here is code: Date date = new Date((long)timestamp*1000); SimpleDateFormat sdf =...
Jon Skeet
people
quotationmark

You're multiplying a timestamp which is already in milliseconds since the Unix epoch by 1000. You just want: Date date = new Date(timestamp); If you look at all of the date, not just the time, you'll see it's currently in 46886! more 12/1/2014 12:57:28 PM

people

How to build a LINQ expression with Contains method from IEnumerable?

I'm trying to build a LINQ expression, to filter values from a int property: protected IQueryable<T> Some(IEnumerable<int> ids) { var parameter =...
Jon Skeet
people
quotationmark

It looks to me like you've just got the arguments the wrong way round. This: Expression.Call(method, property, value) means that you're calling: Enumerable.Contains(x.Col_id, ids) whereas you want Enumerable.Contains(ids,... more 12/1/2014 11:40:18 AM

people

Why do I get an "ArrayIndexOutOfBounds" error in this program?

I have written a program in which the user inputs the dimensions of a two-dimensional array, then the computer prints out the table with those dimensions and fills it with random...
Jon Skeet
people
quotationmark

This is the problem: randomTable[t-1][i+1] When t is 0, that will be accessing randomTable[-1][i + 1] - which can never be valid. Likewise you later go on to t - 3. Additionally, your loop is starting at 0 but then going backwards...... more 11/30/2014 8:35:35 PM

people

is accessed from within inner class, needs to be declared final

final int receiverUserId = sendMessageRequest.getReceiverUserId(); final int senderUserId = sendMessageRequest.getSenderUserId(); final int replyTo =...
Jon Skeet
people
quotationmark

There's nothing being passed here at all (in terms of the variables that the compiler complains about) - and everything is passed by value in Java anyway, whether that's a reference or primitive value. The problem is that you're trying to... more 11/30/2014 5:55:43 PM

people

JAVA Wrong output in object array program (beginner)

I have an assignment to calculate the amount of carbon dioxide produced in a year from a household and compare how recycling can reduce its CO2 Footprint. There are two classes...
Jon Skeet
people
quotationmark

This is the problem: public void calcNetWasteReduction(){ netWasteReduction = grossWasteEmission = wasteReduction; } That's equivalent to: public void calcNetWasteReduction(){ grossWasteEmission = wasteReduction; ... more 11/30/2014 3:35:22 PM

people

C# LINQ query on multidimensional array

how would I use the following LINQ query correctly? I want to create a one dimensional array that contains only values that are greater than 5. I can't understand why it can't...
Jon Skeet
people
quotationmark

The problem is that multi-dimensional (rectangular) arrays implement IEnumerable, but not IEnumerable<T>. Fortunately, you can use Cast to fix that - and Cast gets called automatically if you explicitly specify the type of the range... more 11/29/2014 6:38:20 PM

people

Misunderstanding about Comparator in java 8

public class Test { public static void main(String[] args) { List<Pair<String, Integer>> list = new ArrayList<>(); list.add(new...
Jon Skeet
people
quotationmark

I believe it's just that type inference is failing, basically - because the reverse() call gets in the way of the expected argument type of sorted() and the lambda expression. You can do it if you specify the type to comparingInt... more 11/29/2014 6:14:14 PM

people

Java: Sorting a map

I'm looking for a way to hold scores for players for a highscore. A typical input : map.put(p1, 9); map.put(p2, 7); map.put(p3, 9); Now I need a way of ordering the map so to...
Jon Skeet
people
quotationmark

I would keep the score for the player within the Player object itself. Then you can just create a list of the players, and order it with a Comparator<Player> implementation that orders by scores - either deliberately making it... more 11/29/2014 5:42:12 PM

people

Sort HashMap of ArrayList Based on ArrayList5 Size

I realize there are questions similar to this, but they do not answer my question. I need to return the keys of my HashMap, based on the size of the corresponding value's...
Jon Skeet
people
quotationmark

For Java 7, you can call entrySet() to get a Set<Map.Entry<String,ArrayList<Integer>>> - which you can then use to populate something like an ArrayList<Map.Entry<String,ArrayList<Integer>>> which you can... more 11/29/2014 5:20:56 PM

people

Linq Limiting records gives error for paging?

I want to create pagination my LINQ query gives error: var query = from c in db.Projects.Take(2).Skip(2) orderby c.ProjectId descending select c; gives the following...
Jon Skeet
people
quotationmark

The error describes exactly what is required - your sequence needs to be ordered before skip/take many any sense. You already know what ordering you want - you've just got to make it happen earlier in the logical pipeline than the paging.... more 11/29/2014 10:11:53 AM

people