Make a negative number positive

I have a Java method in which I’m summing a set of numbers. However, I want any negatives numbers to be treated as positives. So (1)+(2)+(1)+(-1) should equal 5.

I’m sure there is very easy way of doing this – I just don’t know how.


Just call Math.abs. For example:

int x = Math.abs(-5);

Which will set x to 5.

Note that if you pass Integer.MIN_VALUE, the same value (still negative) will be returned, as the range of int does not allow the positive equivalent to be represented.


Eclipse doesn’t remember source lookup path

For some reason Eclipse doesn’t remember source lookup path for some java projects, and every time during debugging (after redeploying) I need to press “Edit source lookup path” button and add current project to the list.

Does anyone know how to make it remember selected source location?

EDIT: It is a tomcat project, built with ant. Eclipse version is 3.2.2

.classpath looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="output" path="web/WEB-INF/classes"/>
    <classpathentry kind="lib" path="web/WEB-INF/lib/commons-collections-3.2.jar"/>
    ... bunch of libs here

UPDATE: I found required setting – it is under General Preferences -> Tomcat -> Source path.


Try looking at the Run/Debug configuration for what you are executing, and check the source tab. You can add elements manually, but that is really not recommended.

The elements in the source tab are based on the classpath of the project containing the element that you run. In the case of running tomcat, the source path simply contains all the web projects that are associated with tomcat (and their dependencies).


Persisting data suited for enums

Most projects have some sort of data that are essentially static between releases and well-suited for use as an enum, like statuses, transaction types, error codes, etc. For example’s sake, I’ll just use a common status enum:

public enum Status {
    ACTIVE(10, "Active");
    EXPIRED(11, "Expired");
    /* other statuses... */

    /* constructors, getters, etc. */

I’d like to know what others do in terms of persistence regarding data like these. I see a few options, each of which have some obvious advantages and disadvantages:

  • Persist the possible statuses in a status table and keep all of the possible status domain objects cached for use throughout the application
  • Only use an enum and don’t persist the list of available statuses, creating a data consistency holy war between me and my DBA
  • Persist the statuses and maintain an enum in the code, but don’t tie them together, creating duplicated data

My preference is the second option, although my DBA claims that our end users might want to access the raw data to generate reports, and not persisting the statuses would lead to an incomplete data model (counter-argument: this could be solved with documentation).

Is there a convention that most people use here? What are peoples’ experiences with each and are there other alternatives?


After thinking about it for a while, my real persistence struggle comes with handling the id values that are tied to the statuses in the database. These values would be inserted as default data when installing the application. At this point they’d have ids that are usable as foreign keys in other tables. I feel like my code needs to know about these ids so that I can easily retrieve the status objects and assign them to other objects. What do I do about this? I could add another field, like “code”, to look stuff up by, or just look up statuses by name, which is icky.


We store enum values using some explicit string or character value in the database. Then to go from database value back to enum we write a static method on the enum class to iterate and find the right one.

If you expect a lot of enum values, you could create a static mapping HashMap<String,MyEnum> to translate quickly.

Don’t store the actual enum name (i.e. “ACTIVE” in your example) because that’s easily refactored by developers.


Pure JavaScript code for HTTP Basic Authentication?

Where can I find reference code that implements a HTTP Basic Authentication client in pure JavaScript, suitable for AJAX?

Extra points for code, or pointers to code, that can be used independent of JS toolkits like YUI. No points for Java, Flash/Flex, PHP frameworks, etc.


The five-parameter version of the method allows you to specify the username and password. (WHATWG spec), url, async, username, password)

Tab character in Java

Trying to get a tab character into a JMenuItem using t but it’s not printing.

I bet it’s something really basic I’m missing. Here’s the code

menuItem = new JMenuItem("NewtCtrl + N");



Try this instead:

menuItem = new JMenuItem("New");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

You should also add mnemonics for usability:


See the Java Look and Feel Guidelines for greater clarification, especially volume 1.

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..