Browsing 7239 questions and answers with Jon Skeet

find IndexOf one of few strings, regardless which in C#?

I want to find the first index of "class", "struct" or "interface" in text, I don't care which, is there a way to do this in one expression? PS It seems to me this question...
Jon Skeet
people
quotationmark

I can think of two options here: Regular expressions - fairly straightforward, but remember to quote the matches if you are accepting them as input. Sample code: using System; using System.Text.RegularExpressions; public class Test { ... more 8/31/2015 6:01:41 PM

people

Better to change parent's field or override getter?

Let's say I have a Projectile class which acts as a base class for all projectiles in my game. This contains default values for maximum speed, gravity coefficient, bounce...
Jon Skeet
people
quotationmark

Intuitively, the maximum speed of any particular projectile is unlikely to vary over its lifetime (even in the case where different instances of the same type can have different maximum speeds), therefore I would favour a final field for... more 8/31/2015 10:51:05 AM

people

Date class converts Timestamp wrong

In my application, I am creating a live console where I output messages with their timestamp and contents. From what I read, the approach I am using below with the Date() class...
Jon Skeet
people
quotationmark

The timestamp you've got is already in milliseconds. I don't know which converter you used, but if you put 1440823073243 into epochconverter.com it shows: Assuming that this timestamp is in milliseconds ... and comes up with a... more 8/30/2015 5:19:34 PM

people

how to set utf in HTTP GET, using JAVA

I am receiving special character as "ñ" or "á , é" etc in a response of Get request . The code is the next : HttpURLConnection connection = (HttpURLConnection)...
Jon Skeet
people
quotationmark

Ideally, don't use this code at all - use a JSON parser which accepts an InputStream, instead of reading it line by line. If you need, specify UTF-8 as the encoding to the InputStreamReader, as you're currently just using the platform... more 8/30/2015 4:16:20 PM

people

integer array as arguments while running java jar

I have a simple question.How do i pass integer array as argument while running java -jar sample.jar.The integer array is my input. There are some other options like passing the...
Jon Skeet
people
quotationmark

No, there are no other options really. The command line arguments simply are passed as text. Some code, somewhere, has to parse them into integers. There are various third party libraries available to parse the command line, but your... more 8/30/2015 4:11:57 PM

people

YYMM#### how to generate invoice number

i have to generate invoice number like YYMM#### YY and MM are easy and we can get them using today date but how to add custom 4 digit numbers starts from 001 and end 999 i am...
Jon Skeet
people
quotationmark

If you only have at most 999 invoices per month, you probably don't need to worry two much about the inefficiencies involved in two invoices in quick succession, so each you need to generate an invoice: Work out the prefix to use (be... more 8/29/2015 2:01:19 PM

people

What is the difference between Expression.Variable() and Expression.Parameter()?

Both seem to return the same type, and have the same signature. So what is the difference between them, and when should we use each?
Jon Skeet
people
quotationmark

Expression.Variable is used to declare a local variable within a block. Expression.Parameter is used to declare a parameter for an incoming value. Now currently C# doesn't allow statement-bodied lambda expressions, but if it did,... more 8/29/2015 8:24:33 AM

people

Private field in subclass is accessible in superclass

It is written in JLS (see section 8.3): "A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class....
Jon Skeet
people
quotationmark

It's talking about nested classes - here's an example: public class Test { public static void main(String[] args) { new Subclass(10).foo(); } static class Superclass { private int x; Superclass(int x)... more 8/28/2015 2:51:55 PM

people

Java (BigInteger from byte array)

I'm using following code to create a BigInteger from hexadecimal string and print in to output. package javaapplication2; import java.math.BigInteger; import...
Jon Skeet
people
quotationmark

You're passing in a byte array where the first byte has a top bit that is set - making it negative. From the constructor documentation: Translates a byte array containing the two's-complement binary representation of a BigInteger into... more 8/28/2015 2:35:50 PM

people

Parameter not set error in Prepared statement

I'm trying to execute a prepared statement in JDBC, and everytime I execute it I get the error message "Paramater not set". I have tried repeatedly to check for unset parameters -...
Jon Skeet
people
quotationmark

You're calling prepareStatement twice, setting the parameter on the first one but then calling executeQuery on the second. It's not clear where you're event declaring statement or resultSet, but you want: PreparedStatement preparedStmt =... more 8/28/2015 12:40:49 PM

people