Browsing 7239 questions and answers with Jon Skeet

How to compare string to String Array in C#?

I have an string; String uA = "Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 Twitter for iPhone"; String[] a=...
Jon Skeet
people
quotationmark

So you want the word within the array which occurs earliest in the target string? That sounds like you might want something like: return array.Select(word => new { word, index = target.IndexOf(word) }) .Where(pair =>... more 5/15/2015 3:35:50 PM

people

C# Linq double groupby need min/max for hourly values once per day

I have been playing around with this for awhile now and can't quite get the result I am looking for. I have an object like this: public class Point { public string Tag...
Jon Skeet
people
quotationmark

No need for two statements at all... nor using a string conversion. Just use the key of each group, and the DateTime.Date property: var query = list.GroupBy(x => new { x.Tag, x.Time.Date }) .Select(g => new NewPoint ... more 5/15/2015 1:45:38 PM

people

How to use "iHarder" base 64 encoder on android

This is the Base64 class : http://iharder.sourceforge.net/current/java/base64/ the problem is, i have two Editext and one Button in my activity.so i need to use this for encode...
Jon Skeet
people
quotationmark

Don't use a third-party class in this case - Android already has a Base64 class. Just use Base64.encodeToString(byte[], int) and Base64.decode(String, int). Note that base64-encoding is for binary data, so if your actual source is text,... more 5/15/2015 10:40:08 AM

people

Aes Encryption For Custom(User defined type) class C#

Can I use the Aes Encryption technique to encrypt/decrypt a user defined type. In this link the encryption is done for a simple string. But Suppose I want to encrypt my below user...
Jon Skeet
people
quotationmark

You're currently combining two separable concepts: How do I convert between an instance of Person and a stream of binary data (or byte[])? How do I encrypt/decrypt an arbitrary stream of binary data (or byte[])? Those two concepts... more 5/15/2015 10:15:31 AM

people

TCP Socket on JAVA Any byte >= 128 is received as 65533

I am implementing a server on Android and I am using: while (!Thread.currentThread().isInterrupted()) { try { int r; String response = ""; while ((r =...
Jon Skeet
people
quotationmark

If the client sends me a byte of value 0, it is not received by the server. Yes it is, but you're ignoring it: while ((r = input.read()) > 0) { You probably meant: while ((r = input.read()) > -1) { The second issue is,... more 5/15/2015 10:00:25 AM

people

Mongodb $week and Java Calendar.WEEK_OF_YEAR

Mongodb's $week operator states Takes a date and returns the week of the year as a number between 0 and 53. Weeks begin on Sundays, and week 1 begins with the first Sunday of...
Jon Skeet
people
quotationmark

I would avoid bringing the Java week-of-year into the picture at all - unless you can persuade MongoDB to stop being so broken, somehow. (That's a really odd definition of week-of-year; I'd normally use the ISO-8601 definition.) If you... more 5/15/2015 7:32:00 AM

people

ReentrantLock unlocking in called method

ReentrantLock void a() { lock.lock(); //got intrinsic lock System.out.println(Thread.currentThread().getName()); System.out.println("In A"); b(); ...
Jon Skeet
people
quotationmark

Now I'm intentionally unlocking in b() which releases the lock so that other waiting thread could get the lock No, it doesn't fully release it - it just decrements the lock count. Before you call unlock, you've called lock twice, so... more 5/15/2015 7:12:39 AM

people

Application freezes when trying to read Author from .doc multiple files

I'm trying to make an app that displays all authors of .doc files from a Folder and its subfolders, the problem is that I used Directory.GetFiles("*.doc",...
Jon Skeet
people
quotationmark

the app freeze in this point Yes, because you're doing a lot of work in the UI thread. Don't do that - never do that. You should probably do all that work in a background thread (either with BackgroundWorker or maybe Task.Run) and... more 5/15/2015 7:09:57 AM

people

Error reached end of file while parsing

This is my Java class import java.util.Scanner; public class first { public static void main(String args[]); int right_number, user_input; right_number = 6; ...
Jon Skeet
people
quotationmark

This is the first problem: public static void main(String args[]); You're not actually declaring a method body here. It should be: public static void main(String[] args) { // Method body goes here } You should only use a ; at the... more 5/15/2015 5:57:43 AM

people

Returning Null Value for Unknown Type

I'm trying to write the following method using generics. (My actual method is more complex than this.) public T ParseDictionaryItem<T>(string s, Dictionary<string, T>...
Jon Skeet
people
quotationmark

I would suggest returning a ParseResult<T>, which is defined as something like: public struct ParseResult<T> { // Or an exception, or a way of creating an exception private readonly bool success; private readonly T... more 5/14/2015 2:34:04 PM

people