Browsing 7239 questions and answers with Jon Skeet

Errors that don't exist

Umm. This is awkward but I'm getting some errors that say stuff that is totally untrue. Could someone give me a suggestion on how to fix it. I'm new to java so simple answers...
Jon Skeet
people
quotationmark

This is the problem: public static void choice() { int user_choice = 0; ///This is the method giving me grief!!!!!! user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code... more 3/28/2014 9:41:06 PM

people

Is it good to NULL a Collection object?

At some point in my code I want to nullify a Collection object. Is it possible for memory leak by doing so? Set<String> set = new HashSet<String>("Test"); ... ...
Jon Skeet
people
quotationmark

You don't nullify an object - you change a variable's value to null. It's really, really important that you understand the difference between objects, variables and references. That's rarely useful, but it would be extremely odd to lead... more 3/28/2014 7:35:26 PM

people

How to convert Bit Array to byte , when bit array has only 5 bits or 6bits

I have already wrote a program to this , bits working only if bit array length is multiples of 8 . Can some one help me to get bit array of 5 bits converted into byte both...
Jon Skeet
people
quotationmark

Basically you need to round up the number of bytes needed in your first method: byte[] ret = new byte[(bits.Length + 7) / 8]; bits.CopyTo(ret, 0); Your second method already looks okay at first glance... it certainly fills the right... more 3/28/2014 5:52:30 PM

people

Shift elements of array three places to left within java

In my java program I have an array of length 5, I want to shift the contents of the array 3 places to the left. For example [1,2,3,4,5] would become [4,5,1,2,3]. What would be the...
Jon Skeet
people
quotationmark

Well, you need temporary storage of some description. If you have enough memory, you could do this in one go: int[] buffer = new int[placesToShift]; // Save the start of the array in the buffer System.arraycopy(array, 0, buffer, 0,... more 3/28/2014 5:25:20 PM

people

File is always named with the Path

My File is always named like the path and my additional informations i want to have in the Filename but why is it like that? The Path should be the chosen folder and i want to...
Jon Skeet
people
quotationmark

Rather than using string concatenation, use Path.Combine. Aside from anything else, that will be portable if you ever want to use Mono, too. Oh, and there are simpler ways to create a text file too: using (var writer =... more 3/28/2014 3:59:11 PM

people

Java 8 Method reference with generic types

I am having issues with Java 8 method reference combined with generic types. I have simplified my problem to make it clear where the problem lies. The following code...
Jon Skeet
people
quotationmark

Currently you're trying to use the raw mapper type, which erases all kinds of things. As soon as you start using the generic type, all is fine - and type inference can help you: new Mapper<>(TestEvent::setId); Adding <> is... more 3/28/2014 3:43:35 PM

people

Convert date to yyyy mm dd from c# to sql server

I'm developing a c# program and i need to store some date in a sql server database. In my able I have a column type date where i need to store year, month and day. So I convert...
Jon Skeet
people
quotationmark

Don't convert it to a string at all where you don't need to. Convert from the string you get from the user using DateTime.ParseExact, and then keep it as a DateTime. Use that as a parameter in parameterized SQL to insert it into the... more 3/28/2014 3:12:19 PM

people

Joda DateTime with specified hour:minute, but later than now

I have a string for a time in the format "HH:mm" and I would like to get Joda's DateTime for that in the near future. For instance, if now would be 16:33 of March/28, and I give...
Jon Skeet
people
quotationmark

Well, it sounds like it would be a good idea to separate this into more appropriate types: LocalTime userTime = ...; // Parse the user input DateTime now = DateTime().now(); LocalDate today = now.toLocalDate(); LocalDate resultDate =... more 3/28/2014 2:46:41 PM

people

BufferedReader String check

Could you please recommend the best way how to: check that input.readLine().isEmpty() check that Integer.parseInt(input.readLine()) <10 &&...
Jon Skeet
people
quotationmark

You should call readLine() once and use that input - otherwise you're checking a different line each time! String line = input.readLine(); // readLine returns null for "end of input" if (line == null || line.isEmpty()) { // Report... more 3/28/2014 2:25:00 PM

people

Is an arithmetic shift operator useful?

Assume a language's division operator (/) rounds towards -infinity. Other than potentially increased performance, is there any use in the language including an arithmetic shift...
Jon Skeet
people
quotationmark

Yes - it's useful to make it clear that you're interested in bitshifting operations; that you're thinking of a value as "a sequence of bits" rather than a magnitude. For example, if I'm trying to represent a 32-bit integer in four bytes... more 3/28/2014 2:23:04 PM

people