Skip to content

Tag: java

Scope of do-while loop?

In Java, the body of a do-while loop and the loop condition do not belong to the same scope. So the following code won’t compile: But this code does make sense to me. Also, I cannot find any pitfalls if the body and the condition are in the same scope; since the body will always get executed, and Java d…

How to add Tomcat Server in eclipse

I just installed Java EE plugin in plain eclipse and I am trying to add tomcat server. I opened add new server which showing “Choose the type of server to create” but there is no server list. How can I add tomcat server? Eclipse: Indigo. Answer Do as this: Windows -> Show View -> Servers The…

No Such Element Exception?

So here is my code: For some reason I get a No Such Element Exception I’m not sure why though. Basically my program is searching through two text files – armor.txt and TreasureClassEx.txt. getTreasureClass receives a treasure class from a monster and searches through the txt until it reaches a bas…

In Java, how can I combine two JSON arrays of objects?

I have several string each containing a JSON representation of an array of objects. Here’s an example in code to illustrate, though this is not my actual code (the JSON strings are passed in): I need to combine those two JSON arrays into one large JSON array. I could treat this as a String manipulation …

How to safely save a file to disk in android?

I have written an android app that saves (potentially) large files to the SD Card. Occasionally I get an IOException during the write operation which causes the file to be left in a corrupt state. Based on the answer to this question: Question: How to safely write to a file? the strategy I should use is to cr…

How to break out or exit a method in Java?

The keyword break in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method? Answer Use the return keyword to exit from a method. From the Java Tutorial that I linked to above: Any method declared void doesn’t return a value. It does n…

Getting old data with JPA

I’m getting old data with JPA, even if I disable the cache. I guess is because the resource is configured to be RESOURCE_LOCAL, but I’m not sure. My code that is getting old info about the user: My entity: Anybody has some idea of what is going on? UPDATE 1 The begin, flush and commit methods were…