So I can easily accomplish task to find largest number and then if can be divided by three, print out. But do not know how to find second largest number from users sequence. Thanks for any hints! Answer
Tag: java
Pick a random value from an enum?
If I have an enum like this: What is the best way to pick one randomly? It doesn’t need to be production quality bulletproof, but a fairly even distribution would be nice. I could do something like this But is there a better way? I feel like this is something that’s been solved before. Answer The …
Connect to remote MySQL database through SSH using Java
How can I connect to remote MySQL database through SSH from java application? Small code example is helpful for me and I’d appreciate this. Answer My understanding is that you want to access a mysql server running on a remote machine and listening on let’s say port 3306 through a SSH tunnel. To cr…
Is there a fixed sized queue which removes excessive elements?
I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element. Is there an existing implementation for this in Java? Answer There is no existing implementation in the Java Language and Runtime. All Queues extend AbstractQueue, and its doc clea…
Listen to a shoutcast with Android
since quite some time I’m trying to listen to .pls files (shoutcasts). I have to say that I failed horrible. Since StreamFurious can do it it must be possible. First I tried to connect to the shoutcast via sockets (TCP and UDP) –> failed. I couldn’t even receive one byte from the server. …
Eclipse debugging “source not found”
I just started using Eclipse so go easy on me ;). But when trying to debug a JUnit test case I get a dialog that states the the source is not found when I get to this line in the code in my test method: I know I should probably go and try and download the source from somewhere, but
Get generic type of java.util.List
I have; List stringList = new ArrayList(); List integerList = new ArrayList(); Is there a (easy) way to retrieve the generic type of the …
How do you name a SwingWorker thread? Open to code or best practices
I’m trying to debug a horrible exception that is occurring in a SwingWorker thread. If I could name my SwingWorker threads, then I could better interpret the stack traces I am receiving. It doesn’t seem possible to name a SwingWorker thread (is it?). I’m looking for any best practices or …
Java InputReader. Detect if file being read is binary?
I had posted a question in regards to this code. I found that JTextArea does not support the binary type data that is loaded. So my new question is how can I go about detecting the ‘bad’ file and canceling the file I/O and telling the user that they need to select a new file? Answer For those who …
Java long running task Thread interrupt vs cancel flag
I have a long running task, something like: The task can be cancelled (a cancel is requested and checkIfCancelRequested() checks the cancel flag). Generally when I write cancellable loops like this, I use a flag to indicate that a cancel has been requested. But, I know I could also use Thread.interrupt and ch…