Browsing 7239 questions and answers with Jon Skeet

Getting a weird Android error in my R class

I can't export my project to make an apk because of an error in my gen-com.example.project-R.java class. Here's the snippet of code where the error lies in: public static final...
Jon Skeet
people
quotationmark

It looks like you've tried to define a resource with an ID of 512. You can't do that - it's not a valid identifier. Look at the piece of your XML where you've defined ic_launcher and mr_excuse, and look for 512 near that... then give it a... more 2/23/2014 6:52:56 PM

people

Get all months and years between two datetime

What I need is to get logic on how to get monthname-year between two dates. Dictionary<Monthname,year> GetMonthsandYear(Datetime d1,Datetime d2) ...
Jon Skeet
people
quotationmark

If your actual requirement is "the previous 24 months" then it's much simpler. Just start off with the current month, and get the 1st day of it - then iterate and add 1 month 24 times. Personally I'd return an IEnumerable<DateTime>... more 2/23/2014 5:08:53 PM

people

.NET C# to PHP via local TCP connection slow

I have a .NET C# console application acting as a client and a PHP script acting as a server. Both connect via localhost so there is no internet speed dependency. The problem I...
Jon Skeet
people
quotationmark

Having looked at your PHP code, I suspect this is actually the cause of the speed problem: do{ $buf .= fread($con, 1); if(strlen($buf) == 0) break 2; } while(substr($buf, -4) != "DONE"); Assuming that PHP works the same way as... more 2/23/2014 5:02:42 PM

people

Extracting the first 10 lines of a file to a string

public void get10FirstLines() { StreamReader sr = new StreamReader(path); String lines = ""; lines = sr.readLine(); } How can I get the first 10 lines of the...
Jon Skeet
people
quotationmark

Rather than using StreamReader directly, use File.ReadLines which returns an IEnumerable<string>. You can then use LINQ: var first10Lines = File.ReadLines(path).Take(10).ToList(); The benefit of using File.ReadLines instead of... more 2/23/2014 9:46:34 AM

people

How to deal with multiple enumeration types in one variable

I don't know if this is possible or not, but I'm writing a simple game using XNA and C#, and somehow need one particular variable to be able to handle more than one enumeration...
Jon Skeet
people
quotationmark

It sounds like you probably want a union type for CarriableItem or something similar. For example: public sealed class CarriableItem { public Fruit? Fruit { get; private set; } public Misc? Misc { get; private set; } // Only... more 2/23/2014 9:22:58 AM

people

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException in Finding Max Element

Whenever I am trying to run this code, it gives me out of bound exception. Can anyone point me out what's wrong with it. package com.programs.interview; import...
Jon Skeet
people
quotationmark

This is the problem: if (arr[Arraylength] > tmp) Valid array indexes go from 0 to length-1 inclusive. array[array.length] is always invalid, and on the initial call, ArrayLength is equal to arr.length. It's not clear why you're... more 2/22/2014 9:34:46 PM

people

How can I cast base to a drived class?

Is there a work around that will allow me to cast an object of the base class to an object of the derived class? something like the following B extends A A a = new A(); B b =...
Jon Skeet
people
quotationmark

No, absolutely not. What would you expect the values of any fields declared in B but not in A to be? For example, what would you expect this to do: Object x = new Object(); String text = (String) x; System.out.println(text); An Object... more 2/22/2014 5:19:17 PM

people

Is overloading of the == operator necessary here?

I think that the overloading in code below of the == is unnecessary in the manner shown since == already compares references if not overloaded. Please advise me on the manner. It...
Jon Skeet
people
quotationmark

I think that the overloading in code below of the == is unnecessary in the manner shown since == already compares references if not overloaded. Yes, but the point is that because the operator has overloaded, you can compare for value... more 2/22/2014 4:07:10 PM

people

Copy Constructor with Default Constructor in Java

I need to have a copy constructor in my class as i need to create duplicate objects. I believe that if i will create a copy constructor, I will have to specify the non...
Jon Skeet
people
quotationmark

No, if you want to have both a parameterless constructor and a constructor with parameters, you need to declare them both. It's very easy to declare the parameterless constructor though: public YourClassName() { } The super(); is... more 2/22/2014 3:54:44 PM

people

Eclipse Bar Above Code

Does anyone have a clue how to remove the bar which I circled in red in Eclipse? I have the package explorer view open at the left side and I think it is more than...
Jon Skeet
people
quotationmark

Sure - that's the breadcrumb view. You can toggle it on or off with this button: (I'm struggling to find a menu option to toggle it at the moment, but I'm sure there's one somewhere.) Alternatively, you can hide it in the context menu... more 2/22/2014 3:35:50 PM

people