‘Must Override a Superclass Method’ Errors after importing a project into Eclipse

Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods overriding a superclass method). Go to your project/IDE preferences and set the Java compiler level to 1.6 and also make sure you select … Read more

How to round a number to n decimal places in Java

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output. Example: DecimalFormat df = new DecimalFormat(“#.####”); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); } gives the output: 12 123.1235 0.23 0.1 2341234.2125 EDIT: … Read more

How to directly initialize a HashMap (in a literal way)?

All Versions In case you happen to need just a single entry: There is Collections.singletonMap(“key”, “value”). For Java Version 9 or higher: Yes, this is possible now. In Java 9 a couple of factory methods have been added that simplify the creation of maps : // this works for up to 10 elements: Map<String, String> … Read more

How to add local jar files to a Maven project?

You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included) like this: <dependency> <groupId>com.sample</groupId> <artifactId>sample</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath> </dependency> Update In new releases this feature is marked as deprecated but still working and not removed yet ( You just see warning in the log during maven start). An issue … Read more

How do I create a file and write to it?

Note that each of the code samples below may throw IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling. Note that each of the code samples below will overwrite the file if it already exists Creating a text file: PrintWriter writer = new PrintWriter(“the-file-name.txt”, “UTF-8”); writer.println(“The first line”); … Read more

Is null check needed before calling instanceof?

No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null. The Java 11 Language Specification expresses this concisely in section 15.20.2, “Type comparison operator instanceof”. (Java 17 expresses this less concisely, after the introduction of instanceof patternmatching.) “At run time, the result of the … Read more

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