Browsing 7239 questions and answers with Jon Skeet

How to match a java enum

I've an enum like this: public enum ChartType { TABLE(0, false), BAR(1, false), COLUMN(3, false) private int type; private boolean stacked; ChartType(int type,...
Jon Skeet
people
quotationmark

You should create a static method to retrieve a type by number. With just a few charts like this, it's simplest to do that by just running through all the options. For larger enums, you could create a map for quick lookup. The only thing... more 3/6/2015 5:22:51 PM

people

A Shortcut for c# null and Any() checks

Often in C# I have to do this if(x.Items!=null && x.Items.Any()) { .... } Is there a short cut on a collection ?
Jon Skeet
people
quotationmark

In C# 6, you'll be able to write: if (x.Items?.Any() == true) Before that, you could always write your own extensions method: public static bool NotNullOrEmpty<T>(this IEnumerable<T> source) { return source != null... more 3/6/2015 5:13:20 PM

people

Variable breaking scope

How come this gives a 's' does not exist in the current context error (as expected): public static void Main() { foreach(var i in new[]{1, 2, 3}) { int s = i; } ...
Jon Skeet
people
quotationmark

The first error tells me that s doesn't exist outside of the foreach, which makes sense Indeed - it's right. but the second error says otherwise. No, it doesn't. It's telling you that you can't declare the first (nested)... more 3/6/2015 3:07:18 PM

people

Exception in thread "AWT EventQueue 0" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

What I'm trying to do: Launch a window to request parameters What I've tried: If I remove the vp.wait(), the window disappears. If I remove notify(), the program doesn`t...
Jon Skeet
people
quotationmark

Well here's the problem, in actionPerformed. notify(); You're doing that without a synchronized block, so the thread doesn't own the monitor for this... hence the exception. However, you don't just want a synchronized block, because... more 3/6/2015 2:50:50 PM

people

how to convert byte data which is stored in string variable to string

I have converted a string into byte in java and stored that byte data into a string variable. String s = "anu"; byte[] d = s.getBytes(); String e = Arrays.toString(d); How can...
Jon Skeet
people
quotationmark

Firstly, I'd strongly advise against using getBytes() without a charset parameter. Next, just use the string constructor that takes a byte array and a charset: String s = "anu"; byte[] d = s.getBytes(StandardCharsets.UTF_8); String e =... more 3/6/2015 8:36:37 AM

people

I want to print my name's letters one by one

I want to print my name's letters one by one like so: Result: A Af Afs afsh afsha afshan ..... I've tried this coding but its a simple loop and it showing my complete...
Jon Skeet
people
quotationmark

It sounds like you want to print a substring of your name on each step. So start with the complete name: String name = "Afshan"; and then loop for as many letters as there are (using String.length() to check) and then print the... more 3/6/2015 7:08:37 AM

people

Using one getter with an argument as opposed to multiple getters with no arguments

My first attempt at doing some OOP that isn't simple practice exercises is to make a program in java which stores information about people (like a contact book). Below is a...
Jon Skeet
people
quotationmark

You might want to add that as an option in this case, but I would recommend against it in general. Having used both the java.util.Calendar API (where you specify the field number you want to fetch or update) and the APIs of Joda Time and... more 3/6/2015 6:49:32 AM

people

Phantom generic constraints in C#

I came across this problematic quite often: I like to overload some method with same parameters for different return types, but .NET refuses generic constraints to sealed...
Jon Skeet
people
quotationmark

Here's one slightly different way of approach it: // Constraints just to be vaguely reasonable. public static class Reinterpret<T> where T : struct, IComparable<T> { public T Cast(int value) { ... } public T Cast(uint... more 3/5/2015 9:40:11 PM

people

creating indexer for a class with more than one array?

So I'm reading about indexers and basically understands that if you have a class with an array in it, you can use an indexer to get and set values inside that array. But what if...
Jon Skeet
people
quotationmark

You can't, basically. Well, you could overload the index, making one take long indexes and one take int indexes for example, but that's very unlikely to be a good idea, and may cause serious irritation to those who come after you... more 3/5/2015 8:39:20 PM

people

java variable in loop does not count

I have a problem for the following code. Basically the i in String foreCon does not increment, (stays at 0). so therefore the return String does not show according to the weather...
Jon Skeet
people
quotationmark

You're only incrementing the variable if you don't return immediately from the loop - if the forecast is Sunny, Rain or Cloudy, you hit a return statement and don't increment i. You can just move i++ to the bit of the loop straight after... more 3/5/2015 8:10:47 PM

people