I need a method that can tell me if a String has non alphanumeric characters. For example if the String is “abcdef?” or “abcdefà”, the method must return true. Answer Using Apache Commons Lang: Alternativly iterate over String’s characters and check with: You’ve still one p…
Tag: java
HashMap: One Key, multiple Values
How I can get the third value for the first key in this map? Is this possible? Answer Libraries exist to do this, but the simplest plain Java way is to create a Map of List like this:
How to access a sub-file/folder in Java 7 java.nio.file.Path?
Java 7 introduced java.nio.file.Path as a possible replacement for java.io.File. With File, when I access a file under a specific, I would do: What’s the way to do this with Path? I supposed this will work: But calling parent.toString() seems ugly. Is there a better way? Answer Use the resolve method on…
Problems with making a query when using Enum in entity
I have the following in a Question entity: and I am getting this exception: Exception Description: Error compiling the query [Question.countApproved: SELECT COUNT(q) FROM Question q WHERE q.status = ‘APPROVED’], line 1, column 47: invalid enum equal expression, cannot compare enum value of type [m…
Is it possible to specify multiple email addresses to receive bounce messages?
We’re using JavaMail API to send emails from our application. To handle bounce back messages (Non delivery report), we’re redirecting bounce backs to a different email address using the following code: In our case, we want bounce backs to be redirected to multiple email addresses. In fact, we even…
Netbeans IDE – Automatic Suggestion while typing
hi i am using netbeans 7 IDE for java programming , and i am acutally a C# programmer and in visual studio whenever i type anything it displays a dropdown menu with suggestions , i want that to be enabled on netbeans IDE without having to press CTRL + Space to show the dropdown menu thanks in advance . Answer
Closing BufferedReader and System.in
I’ve noticed that if I close the BufferedReader, I won’t be able to insert input from the keyboard anymore, as System.in is somehow closed. Is there anyway I can keep br.close() (I need that in order to delete a file) and then add more input from the keyboard? Answer Looks like you need: http://co…
How to convert year month day to proper UTC milliseconds from the epoch?
I am trying to convert a date into milliseconds with the following code: I get left=-2206310400000, but when I check here, I should get -2208988800000. What am I doing wrong? Answer You’re using 1 for the month number, which means February. You mean From the docs: month – the value used to set the…
What is the reason for OutOfMemoryError: Java heap space in the following case?
The following code sample is inside a for loop that runs about 2 million times. stack trace: Please let me know if more information is required. Thank you. Thanks everyone for the answers. I will accept BalusC’s answer since he answered first. Unfortunately I cant upvote any other answers due to not eno…
Probability in Java
I was curious to know, how do I implement probability in Java? For example, if the chances of a variable showing is 1/25, then how would I implement that? Or any other probability? Please point me in the general direction. Answer You’d use Random to generate a random number, then test it against a liter…