This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Cologne, Germany
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephan van Hulst

I just realized you can simplify the regular expression even further.

Let's say you have the string "aaa-bbb". The first "a" obviously matches the character class [a-z], and the second "a" matches the character class [a-z ' -]. There is absolutely no way to tell if the third "a" matches the [a-z ' -] of the old group, or the [a-z] of a new group.

This means that the following regular expression matches the same strings as your original:
1 hour ago
How do you figure the identity is 1? The first argument you pass to reduce() is literally 0.
If we first sort the three elements, we end up with the following sequence: ["Linux", "Mac", "Windows"].

At what index would we put "Max" if we inserted it into this sequence? After "Mac", so at index 2.

Apply the formula to the insertion index: (-2 - 1) == -3.
In addition to all that Tim said, it makes no sense to use both uppercase and lowercase letters in your character classes if you're going to use the CASE_INSENSITIVE option.

If we simplify your regex a little bit, we end up with this (I added spaces for clarity, you can ignore them using the COMMENTS option):

We can further simplify this regex, because "X (X)*" is the same thing as just "(X)+". Here, "X" is "[a-z][a-z ' -]+":

This regular expressions means:

The beginning of the string, followed by a one or more groups of (a letter followed by one or more letters, quotes or dashes).

If we use Tim's suggestion to use the Unicode "letter" category instead of just ASCII letters, the final result looks like this:
7 hours ago
I don't know Tapestry, and I absolutely despise Bootstrap, so I'm not sure if this will solve your problem, but...

A quick google search indicates that the Form component from Tapestry has an autofocus property that defaults to true. I guess all you need to do is set it to false.

Carey Brown wrote:They ARE clearly indicated because they only reside inside the KeyboadInputs utility class.

Yes, but you won't be able to tell that your code is using the KeyboardInputs class just by looking at method signatures. This can lead code maintainers to make wrong assumptions about code they didn't write, and make it difficult to test and easy to introduce new bugs.

Even if you're writing code just for yourself, and you think passing services to constructors/methods is onerous, I don't think it's a good thing to teach this to beginners.

Do you not consider STDIN a constant?

No. Standard input produces different results upon different method calls. By definition, it is not constant. Assigning non-value types to globally accessible variables is one of the biggest contributors to buggy spaghetti code.

Campbell Ritchie wrote:How could you avoid that? If anybody writes new Scanner( anywhere, you are up the creek without a paddle anyway.

Yes, and it's a shame that the Java designers chose to make standard input available through a static field, instead of an application context injected into the main method. But is that a good argument for adding even more static references to non-value types to your application? You're just adding different ways for your application to access standard input unexpectedly.

Can either of you tell me why you think this is so terrible?
1 day ago
It's NOT a byte array. It's a string. It has a private "value" field that's a byte array, but you don't need to know that (in fact, it's apparently only serving to confuse you).

The reason your temp variable is a byte array is because you're converting the string "2" to a byte array using the getBytes() method. Why? What's the point of that?

If you want to read an integer using a scanner, just call nextInt().

I strongly advise against reading a CSV file using a Scanner though. CSV files can be more complex than you think. Use a CSV parsing library.
1 day ago
So why are you worried about somebody changing the delimiter when nobody is watching, but not worried about somebody consuming input using your utility class when nobody is watching?
1 day ago
If the name "De Almeida" means anything to you, I suggest you remove that hex string from your post.
Personally I think this is not a good pattern to emulate.

Yes, it's true that you should only ever wrap one buffered I/O object around, but making a static field out of it is NOT a good idea.

Static fields should only ever be used for real constant values, or for cross-cutting services such as loggers. Using them for business logic services hides dependencies and makes your application harder to test and debug.

You really MUST pass the scanner into all methods and constructors that need it. While this may seem messy to you, it really is not as bad as it sounds, and it will result in code that clearly indicates which parts require I/O.
1 day ago
Yes it can, but personally I feel plain JAX-RS is much more elegant for creating a simple REST API.

Why don't you give it a try?
1 day ago
All answers are wrong.

The real answer is: "It depends on the Java implementation". The normalize() method has no fixed definition.

It's likely that getNameCount() will return 1, but keep in mind that it is not guaranteed.

Here's the explanation: Paths don't care what is on your hard drive or what directory you are currently in. Except for only a few methods, none of the methods on Path will access your file system. A path like Paths.get("/i/like/puppies") is really just a thin wrapper around the string "/i/like/puppies", and it doesn't care whether the names between the slashes refer to real directories. Maybe it will make it easier for you to think of a path as if it was really just a string, and the methods on Path are really just string operations.

So, the method Path.normalize() is really just a string operation, and it doesn't look at your current working directory. In most cases (but not all!), this operation will just remove the name "." from your path, and it will remove ".." and the preceding directory name from your path. So, for the path Paths.get("."), it will just remove the ".", and leave you with the empty string.

This means that on most Java implementations, Paths.get(".").normalize() returns the same path as Paths.get(""). Paths.get("") has one path element (the empty string), so its name count is 1.

What book are you using? This particular question seems of low quality, because not only is the answer they give wrong, the code is poorly written as well. This whole piece:

Can be replaced with just:
Can you explain the differences between classes and functions, and how you use them? If so, you should be able to answer this question relatively easily.
3 days ago
The + operator is left-associative, meaning that:
would be equal to:
which would be equal to:

Please tell us what the result of this operation is supposed to be.
4 days ago