How to cast a double to an int in Java by rounding it down?

I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99


Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)

Simply typecast with (int), e.g.:

System.out.println((int)(99.9999)); // Prints 99

This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (@Chris Wong)


Is it possible to change the element id separator in JSF?

For example, the following snippet:

<h:form id="levelone">
    <h:inputText id="leveltwo" value="Test" />

generates the following markup:

<form id="levelone" name="levelone" method="post" action="/test/testPage.html" 
   <input id="levelone:leveltwo" type="text" name="levelone:leveltwo" 
          value="Test" />

Is it possible to change the automatically generated ids to use a different separator than colon?

For example, I’d like to change





We’re using the Mojo JavaScript application framework in our webapp, and it doesn’t seem to like the colons in the id’s.


This is not possible in JSF 1.x, but since JSF 2.x you can define it in web.xml as init-param of javax.faces.SEPARATOR_CHAR.

That said, I guess that you just wanted to change it because you’d like to get your CSS to work, is it? The colon : is namely a special character in CSS identifiers, it represents a pseudo selector. If this reason is true for you, then it might be good to know that you can escape special characters in CSS the usual way by .

Thus, e.g.

#levelone:leveltwo {
    color: blue;

ought to work for normal browsers (for IE6/7 you need #levelone3A leveltwo instead).

The same applies when you intend to use it with jQuery or any other JavaScript framework which selects elements with help of CSS selectors:

var leveltwo = $('#levelone\:leveltwo');

Alternatively, you can also just give it a styleClass which you in turn can correlate with a CSS class. Thus, e.g.

<h:inputText styleClass="myinput" />

which generates

<input type="text" class="myinput" />

can be styled with

.myinput {
    color: blue;

See also


resettable timeout in Java

(similar to “Resettable Java Timer” but there are some subtleties I need to explore)

I need a resettable timeout feature, so that if my class does not perform a particular action within an interval of time T0 (where T0 is in the neighborhood of 50-1000msec), then a method gets called:

class MyClass {
    static final private timeoutTime = 50;
    final private SomeTimer timer = new SomeTimer(timeoutTime, 
        new Runnable () { public void run() {

    private void onTimeout() { /* do something on timeout */ }

    public void criticalMethod() { this.timer.reset(); }

What can I use to implement this? I’m familiar with ScheduledExecutorService, and the idea of calling ScheduledFuture.cancel() and then rescheduling the task seems like it should work, but then there’s a potential hazard if cancel() fails and the scheduled task executes when it shouldn’t. I feel like I’m missing a subtlety here.

Also (perhaps more importantly), is there a way to test my implementation / prove that it works properly?

edit: I am particularly concerned about the case where criticalMethod() gets called often (perhaps several times per millisecond)… if I use ScheduledExecutorService, it just seems like a potential resource problem to keep creating new scheduled tasks + canceling old ones, rather than having a direct way to reschedule a task.


The cancelled attribute is attached to the task object. So, either the task hasn’t started when you call cancel, and it won’t get run; or the task has already started when you call cancel, and it gets interrupted.

How to handle interruption is up to you. You should regularly poll Thread.interrupted() (which, by the way, resets the interrupted flag, so beware) if you aren’t calling any interruptible function (ones that declare InterruptedException in their throws clause).

Of course, if you are calling such functions, you should handle InterruptedException sensibly (which includes reasserting the interrupted flag (Thread.currentThread().interrupt()) before your task returns). 🙂

To answer your edit, object creation is cheap, as long as your object doesn’t have a lot of state. I personally wouldn’t worry about it too much unless profiling shows it to be a bottleneck.


Is JDEE worth using?

I see many nice feature of JDEE in Emacs. However installation seems to be a bit involved, especially in Windows so I want to see if others found it useful. I use Eclipse and NetBeans and there are some decent features to these products. However, I really like the idea of a scripted language like Lisp built into my IDE so I can change most features on-the-fly.

So I want to give JDEE a shot, but I’ve heard from more than one advanced Emacs user that they don’t even need JDEE. I wonder if those people even tried JDEE or if they are just doing simple Java projects. Has anyone tried JDEE and liked it? Are there features in Emacs that make JDEE fairly pointless? Please no “try Eclipse” comments..I have used it and it has nice features, but I want to give Emacs a fair shot.

UPDATE: See my accepted answer. I tried JDEE for a while but gave it up for eclipse and have never looked back. Happily ever after.


A year an a half later, I can honestly say that it was not worth the effort. I know emacs fairly well and was hoping that I could get more efficient. However the level of growth and support for the modern IDE’s make using them the best choice. Eclipse was a bit unstable when I first wrote the above post, but so was JDEE but now Eclipse is better and JDEE is still a bit stagnant.

Anyway, Emacs is a great tool and has great features, but it has recently fallen out of the radar for most developers. Eclipse is updated all the time and does exactly what I need. Advice to those considering JDEE: invest your precious time learning Eclipse and you will get much more efficiency. Emacs fans, go ahead and flame me but I was just like you and now I have more time and better code and I want others to have the same result.


Javascript Regex literal with /g used multiple times

I have a weird issue working with the Javascript Regexp.exec function. When calling multiple time the function on new (I guess …) regexp objects, it works one time every two. I don’t get why at all!

Here is a little loop example but it does the same thing when used one time in a function and called multiple times.

for (var i = 0; i < 5; ++i) {
  console.log(i, (/(b)/g).exec('abc'));

> 0 ["b", "b"]
> 1 null
> 2 ["b", "b"]
> 3 null
> 4 ["b", "b"]

When removing the /g, it gets back to normal.

for (var i = 0; i < 5; ++i) {
  console.log(i, (/(b)/).exec('abc'));
}             /* no g ^ */

> 0 ["b", "b"]
> 1 ["b", "b"]
> 2 ["b", "b"]
> 3 ["b", "b"]
> 4 ["b", "b"]

I guess that there is an optimization, saving the regexp object, but it seems strange.

This behaviour is the same on Chrome 4 and Firefox 3.6, however it works as (I) expected in IE8. I believe that is intended but I can’t find the logic in there, maybe you will be able to help me!



/g is not intended to work for simple matching:

/g enables “global” matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.

I’d imagine internally javascript holds the matching after the capture, so it would be able to resume matching and therefore null is returned since b occur only once in the subject. Compare:

for (var i = 0; i < 5; ++i) {
  console.log(i +'    ' + (/(b+)/g).exec("abbcb"));


0 bb,bb
1 b,b
2 null
3 bb,bb
4 b,b
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..