How do I prevent String.format(“%.2f”, doubleValue); from rounding off (round half up algorithm) instead of just truncating it? e.g. after formatting, I just want to discard the last digit, I know there are other ways to do this, I just want to know if this is possible using the String.format. Ans…
Tag: java
How do I convert from int to Long in Java?
I keep finding both on here and Google people having troubles going from long to int and not the other way around. Yet I’m sure I’m not the only one that has run into this scenario before going from int to Long. The only other answers I’ve found were “Just set it as Long in the first p…
C# Java HashMap equivalent
Coming from a Java world into a C# one is there a HashMap equivalent? If not what would you recommend? Answer Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java’s Map interface). Some notable…
Java UTF-8 strange behaviour
I am trying to decode some UTF-8 strings in Java. These strings contain some combining unicode characters, such as CC 88 (combining diaresis). The character sequence seems ok, according to http://www.fileformat.info/info/unicode/char/0308/index.htm But the output after conversion to String is invalid. Any ide…
Play WAV file backward
I’m making Braid in Java. If you rewind the time, the sound plays backward. How to play a WAV file backwards? Maybe with a stream with something like previous()? On the site of Braid can you see what I mean. Update: Solved! See my own post. Answer I solved it by myself And then:
How to wait for a number of threads to complete?
What is a way to simply wait for all threaded process to finish? For example, let’s say I have: How do I alter this so the main() method pauses at the comment until all threads’ run() methods exit? Thanks! Answer You put all threads in an array, start them all, and then have a loop Each join will …
Is *this* really the best way to start a second JVM from Java code?
This is a followup to my own previous question and I’m kind of embarassed to ask this… But anyway: how would you start a second JVM from a standalone Java program in a system-independent way? And without relying on for instance an env variable like JAVA_HOME as that might point to a different JRE …
Most efficient way to create InputStream from OutputStream
This page: http://blog.ostermiller.org/convert-java-outputstream-inputstream describes how to create an InputStream from OutputStream: Other alternatives are to use PipedStreams and new threads which is cumbersome. I do not like the idea of copying many megabytes to new in memory byte array. Is there a librar…
Modifying a file inside a jar
I would like to modify a file inside my jar. Is it possible to do this without extracting and re jarring, from within my application? File i want to modify are configuration files, mostly xml based. The reason i am interested in not un jarring is that the application is wrapped with launch4j if i unjar it i c…
How do I find out if first character of a string is a number?
In Java is there a way to find out if first character of a string is a number? One way is and do the above all the way till 9, but that seems very inefficient. Answer Note that this will allow any Unicode digit, not just 0-9. You might prefer: Or the slower regex solutions: However, with any of these