Code Answers

Java lower bound wildcards

I'm struggling to get my head around this and was wondering if someone could explain the reasons for this. I have three classes: class Angel {} class Person extends Angel {} class Employee extends Person {} When I attempt to execute this code public static void insertElements(List...

Java close PDF error

I have this java code: try { PDFTextStripper pdfs = new PDFTextStripper(); String textOfPDF = pdfs.getText(PDDocument.load("doc")); doc.add(new Field(campo.getDestino(), textOfPDF, Field.Store.NO, Field.Index.ANALYZED)); } catch (Exception exep) { System.out.println(exep); System.out.pr...

Java – failed to load class ‘org.slf4j.impl.StaticLoggerBinder’

I created a jar file with all the dependencies, and I implemented the logging using slf4j + logback. Thus I have a logback.xml file defined for all the loggers. However when I run the jar it gave me this message: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J:...

Java – xstream CannotResolveClassException

I'm trying to use xstream 1.4.2 to convert xml to object. It does work perfectly fine for me until I put the object's class file in a separate package than where the main code runs. Then I get a CannotResolveClassException. I've tried using the setClassLoader method as recommended by others but that doesn't help. Exception in thread "main" com....

Java – Pagination in Java

I wrote paging logic: My requirement: total elements to display:100 per page,if i click next it should display next 100 records,if i click previous 100 records. Initial varaible values: showFrom:1, showTo:100 max elements:depends on size of data. pageSize:100. Code: if(paging.getAction().equalsIgnoreCase("Next")){ paging.setTotalRec(availa...

Java Math.min/max performance

EDIT: maaartinus gave the answer I was looking for and tmyklebu's data on the problem helped a lot, so thanks both! 🙂 I've read a bit about how HotSpot has some "intrinsics" that injects in the code, specially for Java standard Math libs (from here) So I decided to give it a try, to see how much difference HotSpot could make against doing the c...

Java – how to extract a substring from a string in java

dear all, i have a string like this, "…1name: john, 2name: lice, 3name: mike…". i want to output its substring "1name: john". its position in the string is not fixed. i also use the substring method but can not get it. so could you give me a help. thank you. Solution to answer: String s = str.split(",")[n].trim(); I suggest making a ma...

Java : Unparseable date Exception

I have this value, 2011-11-19T00:00:00.000-03:00 which needs to be converted and set it in this format yyyyMMdd I tried this way import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class ParsedDate { public static void main(String arg...

Java.lang.Object VS java.util.Objects,what’s the difference

As we all know,Object is the root class in Java. I found a class named Objects that looks quite similar toObject. TheisObjects class has confused me for a while. Can anybody tell me when and where we should use the Objects class? Solution to answer: Objects simply contains a set of utility methods that are useful in combination with Obj...

Java warning with implementing Comparable

I'm trying to use Collections.sort on a ArrayList of custom objects, but I'm getting a warning and I can't figure out why Warning: Type safety: Unchecked invocation sort(ArrayList) of the generic method sort(List) of type Collections With this code: ArrayList charOccurrences = new ArrayList(); ... Collections.sort(charOccurrences); And he...

Java 7 – Multiline strings

I've read that multiline string literals were proposed to be added in Java 7. Although I can't find any documentation saying definitely that they have been. I'd like to know if they are, because this is something I'd consider switching versions for. Solution to answer: Multiline string literals are not going to be added to JDK 7. You ca...

Java—how can I dynamically reference an object’s property

In javascript, I can do this: function MyObject(obj) { for (var property in obj) { this[property] = obj[property]; } } Can I do anything close in Java? class MyObject { String myProperty; public MyObject(HashMap props) { // for each key in props where the key is also the name of // a property in MyObjec...

Java – Cannot Find Symbol

I tried compiling my personal contacts database program, but I have nothing but "cannot find symbol" errors Should I be instantiating an object more often than once? If not, what is wrong with this code? I moved writetoFile and readFile initializations to outside try block, and declared them inside, and it helped! Now the only compiler error...