Java way to check if a string is palindrome

Using reverse is overkill because you don’t need to generate an extra string, you just need to query the existing one. The following example checks the first and last characters are the same, and then walks further inside the string checking the results each time. It returns as soon as s is not a palindrome. The problem with the reverse approach … Read more

Java Hashmap: How to get key from value?

If you choose to use the Commons Collections library instead of the standard Java Collections framework, you can achieve this with ease. The BidiMap interface in the Collections library is a bi-directional map, allowing you to map a key to a value (like normal maps), and also to map a value to a key, thus allowing you to perform … Read more

How to check if an int is a null

An int is not null, it may be 0 if not initialized. If you want an integer to be able to be null, you need to use Integer instead of int. Besides the statement if(person.equals(null)) can’t be true, because if person is null, then a NullPointerException will be thrown. So the correct expression is if (person == null)

catDog string problem at Codingbat.com

While you are pretty close to a solution, there are a few critical problems in your code: Your call to substring() fetches a string of size 2. That string can never be cat nor dog. Change the second parameter in the method call to i + 3 to get 3 characters. Your call to substring() will throw an IndexOutOfRangeException, as i approaches the end of the length of the input string. Make … Read more

Error: Could not find or load main class in intelliJ IDE

This might help: 1) “Build” menu -> “Rebuild Project“. Sometimes Intellij doesn’t rewrite the classes because they already exist, this way you ask Intellij to rewrite everything. 2) “Run” menu -> “Edit configuration” -> delete the profile -> add back the profile (“Application” if it’s a Java application), choose your main class from the “Main … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)