Skip to content

Tag: java

How to test if a double is an integer

Is it possible to do this? I know the code probably doesn’t go anything like that, but how does it go? Answer This checks if the rounded-down value of the double is the same as the double. Your variable could have an int or double value and Math.floor(variable) always has an int value, so if your variab…

How do you get absolute values and square roots

How do you get a square root and an absolute value in Java? Here is what I have: But is there an easier way to get the absolute value in Java? Answer Use the static methods in the Math class for both – there are no operators for this in the language: (Likewise there’s no operator for raising a val…

Parsing prices with Currency Symbol in Java

I want to parse a String that i have into a Number. This is the Code that i’m using but not working: This results in a java.text.ParseException So i want to match the String into a number, i don’t really care about the currency, but it would be nice to have. I want the following kind of Strings ma…

How to lock aspect ratio of a gridLayout in Java?

Is there an easy way of locking the aspect ratio of a GridLayout component in Java Swing ? Or should this be done on the JPanel containing that layout ? Answer GridLayout effectively ignores a component’s preferred size, but you can control the aspect ratio of whatever is drawn in paintComponent(), as s…

Removing time from a Date object?

I want to remove time from Date object. But when I’m converting this date (which is in String format) it is appending time also. I don’t want time at all. What I want is simply “21/03/2012”. Answer The quick answer is : No, you are not allowed to do that. Because that is what Date use …

Java ClassLoader: load same class twice

I have a ClassLoader which loads a class compiled by JavaCompiler from a source file. But when I change the source file, save it and recompile it, the ClassLoader still loads the first version of the class. What am I missing? like a newInstance or something? Answer A classloader can’t replace a class th…

How can this Java tree be 1000 x faster?

I am programming an AI for a chess-like game, based on two types of pieces on a 8 x 8 grid. I want to build a kind of minmax tree, which represents each possible move in a game, played by white players in first, and by black players in second. I have this generate() method which is call recursively. I