We are given a string, say, “itiswhatitis” and a substring, say, “is”. I need to find the index of ‘i’ when the string “is” occurs a second time in the original string. String.indexOf(“is”) will return 2 in this case. I want the output to be 10 in this case. Answer Use overloaded version of indexOf(), which takes the starting index
Java-get most common element in a list
Does Java or Guava have something that will return most common element in a list? [1,3,4,3,4,3,2,3,3,3,3,3] return 3 Answer This is fairly easy to implement yourself: 3 If you want to handle cases where there’s more then one most frequent element, you can scan the list once to determine how many times the most frequent element(s) occur, and then scan
Can’t find @Nullable inside javax.annotation.*
I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot find symbol Answer You need to include a jar that this class exists in. You can find it here If using Maven, you can
How to use interface to communicate between two activities
I am trying to make listener interface between two activities Act1 and Act2. Act1 will start Act2. If there is some event occurred in Act2, it will inform it to Act1. Problem is that I am starting new activity using Intent, so how Act1 will assign itself as listener to Act2’s interface? Act1.java Act2.java Note: Please don’t suggest me to
How can we use .class on primitive types?
When we say it prints class java.lang.Integer which makes sense because java.lang.Integer is a class. So we can have a corresponding Class object. But when I do it prints int which I felt is kind of ambiguous as .class returns an object of type Class and int is not a class (but a primitive type). What is the motive behind
Save drop-down list value to database in Struts 2 with Hibernate
I want to save the selected value of drop-down list into the database. First index.jsp is loaded. From index.jsp, we can go to register.jsp when we click register URL of index.jsp. struts.xml: index.jsp: register.jsp: Action class is: the drop down list is actually only one of the form fields. there are other fields also in the form. All values other
How do I change the IntelliJ IDEA default JDK?
I use IntelliJ IDEA as my development environment, and Maven for dependency management. I frequently build my project structure (directories, poms, etc) outside of IDEA and then import the project into IDEA using Import project from external model. This works great, except that in my poms I specify that the maven-compiler-plugin should use JDK 1.6, and when I import, IDEA
The type HashMap is not generic; it cannot be parameterized with arguments
This is a strange error i am getting today when i try to implement a Map as below. I am using JDK 1.7 and not sure why this error has been coming and changing the above line by adding cast removes the error. I looked at the related posts in stackoverflow before posting this question seems to be strange issue.
How to change a package name in Eclipse?
In Eclipse I have a simple Java project that contains a package named (default package) and inside this package I have a class. I want to rename this package into something like: com.myCompany.executable I tried to select the (default package) —> right click —> refactor but now I see only the single voice named: Infer generic type arguments but not
HQL unexpected AST node: :
My code is failing to inject the below repository. Based on the stack trace, it seems my HQL syntax is invalid, but I’m not sure what I’ve done wrong. stack trace: Update: I’m also getting this error with this query: I updated my versions to the latest: Update: I’m also getting this error with: Answer Sadly, you cannot do :order.inProduction