Browsing 7239 questions and answers with Jon Skeet

Error with bracket placement

I am getting this error : Syntax error on token "}", { expected after this token I am not sure what the problem is here and I've checked that the brackets match, or so I think,...
Jon Skeet
people
quotationmark

Look at your code in the problematic area: private void move(){ if(!instance.getStage().isCollided(hitbox)){ directionY = 1; } else { directionY = 0; hitbox.x += directionX; hitbox.y += directionY; ... more 10/30/2014 10:41:25 PM

people

IComparer<T> child as a generic param could not be used in Sort

Got stuck trying to sort my List<> in C#. I have an interface type with its implementation class: public interface IRoom { // ... } public class Room : IRoom { //...
Jon Skeet
people
quotationmark

You're trying to use generic contravariance for this (if a comparer can compare two TRoom values, it should be able to compare two IRoom values), which would usually be fine - but only when TRoom is known to be a class type. You can fix... more 10/30/2014 10:12:21 PM

people

Java convert Byte Array of ASCII values to int

After reading a ByteBuffer into b_array I am trying to convert the ascii values to int. Output I am expecting is 129 after executing the code as (b_array[] has the decimal...
Jon Skeet
people
quotationmark

Your current approach is effectively treating the three values as a 24-bit number - effectively 0x313239. It sounds like you should be converting it into a string, then parsing that: String text = new String(b_array,... more 10/30/2014 8:13:50 PM

people

Separate List(Of Tuple(Of String, Integer)) to get two List(Of String) matching/not predicate

I need to separate a List(Of Tuple(Of String, Integer)) to get two List's(Of String). If the String matching some function ValidateFormat(returns Boolean), then I need it in...
Jon Skeet
people
quotationmark

I'd use something like: var lookups = initial.ToLookup(x => ValidateFormat(x.Item1), x => x.Item2); var match = lookups[true].ToList(); var notMatch = lookups[false].ToList(); That checks each item once, splitting the collection... more 10/30/2014 5:59:55 PM

people

%X format specifier to represent an integer

I've been taking a look a Zed A. Shaw's "Learn C the hard way" tutorials. One of his extra credit questions (in exercise 9) requested me to represent a char[4] as an int. After...
Jon Skeet
people
quotationmark

However after searching I couldn't find any explanation as to why %X would return 123. It's just printing the hex representation of the number. Hex 123 = decimal 291. From a printf man page (the first I happened to find): o, u, x,... more 10/30/2014 5:54:58 PM

people

How to Randomly pick a color from an array of colors in Java?

I'm learning Java and programming for the first time. I'm using the BlueJ environment. I'm writing a program to draw polygons. I want to change color after every line has been...
Jon Skeet
people
quotationmark

The Color fields are already of type Color - you don't need to call the Color constructor. The compiler is complaining because there isn't a Color constructor which takes a Color parameter. I suggest you use the more conventionally-named... more 10/30/2014 5:43:50 PM

people

Can Json.NET serialize to stream with Formatting?

When using Json.NET library, you can specify formatting when you are serialising to string, but I can't find this option when serialising directly to stream. Am I missing...
Jon Skeet
people
quotationmark

I haven't tried it, but I'd expect it to be fine if you specify the formatting in the settings: public static void SerializeToStream(MyObject obj, Stream stream) { var settings = GetJsonSerializerSettings(); settings.Formatting =... more 10/30/2014 5:39:24 PM

people

Adding chars to a string

import java.util.Random; public class PasswordRandomizer { // Define the variables private Random random = new Random(); private int passwordLength; private...
Jon Skeet
people
quotationmark

This is the problem: int j = random.nextInt(); char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j); The charAt method requires that its argument is within the bounds of the string - you're just using a random integer from... more 10/30/2014 5:30:09 PM

people

Dictionary get item with highest value and key

In my dictionary(int,int) I'm trying to get the key where the value is 3 and the key is the biggest. If I want to get the key where the value is 3 I can use this code: var max =...
Jon Skeet
people
quotationmark

The simplest approach is just to use Max: var max = dictionary.Where(x => x.Value == 3).Max(x => x.Key); If you want to get more than just the highest key, you'd use something like: var topTwo = dictionary.Where(x => x.Value... more 10/30/2014 4:12:39 PM

people

Have Relative Paths as properties in property file

So my question is NOT how to load properties file from relative path, but rather how can I declare relative paths (relative to the properties file) as properties path in the...
Jon Skeet
people
quotationmark

You need to understand that Java properties files are just lists of key/value pairs. How you interpret those values is up to you. So you should expect that properties.getProperty("file1") will give you the string "./File1"... but you can... more 10/30/2014 3:49:51 PM

people