Category Archives: java

How do I copy a 2 Dimensional array in Java?

Questions: I need to make a copy of a fairly large 2 dimensional array for a project I am working on. I have two 2D arrays: int[][]current; int[][]old; I also have two methods to do the copying. I need to copy the array because current is regularly being updated. public void old(){ old=current } and… Read More »

Raising a number to a power in Java

Questions: Here is my code. For some reason my BMI is not calculated correctly. When I check the output on a calculator for this : (10/((10/100)^2))) I get 1000, but in my program, I get 5. I’m not sure what I am doing wrong. Here is my code: import javax.swing.*; public class BMI { public… Read More »

Using JAXB to cross reference XmlIDs from two XML files

Questions: I’m trying to marshal/unmarshal from two different XML files to POJOS. The first XML file looks like this: –Network.xml– <Network> <Nodes> <Node id=”ROD” /> <Node id=”KFI” /> <Node id=”JND” /> </Nodes> <Arcs> <Arc fromNode=”ROD” /> <Arc fromNode=”JND” /> </Arcs> </Network> ——— Using @XmlID and @XmlIDREF annotations, I can successfully populate the Arc classes to… Read More »

Change ListView background – strange behaviour

Questions: I have a problem with changing the background of a view in a ListView. What I need: Change the background image of a row onClick() What actually happens: The background gets changed (selected) after pressing e.g. the first entry. But after scrolling down the 8th entry is selected too. Scroll back to the top… Read More »

Calling a servlet from JSP file on page load

Questions: Can I call a servlet from JSP file without using a HTML form? For example, to show results from database in a HTML table during page load. Answers: You can use the doGet() method of the servlet to preprocess a request and forward the request to the JSP. Then just point the servlet URL… Read More »

Getting an attribute value in xml element

Questions: I have an xml string like this and I want to get attribute value of “name” in a loop for each element. How do I do that? I am using javax.xml.parsers library. <xml> <Item type=”ItemHeader” name=”Plan Features” id=”id_1″/> <Item type=”Deductible” name=”Deductible” id=”a”>Calendar Year <Item type=”Text” name=”Individual” id=”b”>200</Item> <Item type=”Text” name=”Family” id=”c”>350</Item> </Item> <Item lock=”|delete|”… Read More »

Can't transparent and undecorated JFrame in JDK7 when enabling nimbus

Questions: Look at this picture : here is the code that transparent’s the frame: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { System.err.println( “Translucency is not supported”); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); this works good but when trying to enable LookAndFeel by adding try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if (“Nimbus”.equals(info.getName())) {… Read More »

Replace last part of string

Questions: I want to replace the last String which is a , with ) Suppose the string is: Insert into dual (name,date, to be converted to: Insert into dual (name,date) Answers: The following code should replace the last occurrence of a ‘,’ with a ‘)’. StringBuilder b = new StringBuilder(yourString); b.replace(yourString.lastIndexOf(“,”), yourString.lastIndexOf(“,”) + 1, “)”… Read More »

Java – char, int conversions

Questions: In Java, the following is allowed: char c = ‘A’ + 1; Here, c will hold the value ‘B’. Above, first the expression is evaluated. So ‘A’ gets converted to 65, the whole expression evaluates to 66, and then 66 is converted to ‘B’ since we are storing the value in a char. The… Read More »