Counts the number of matching brackets in Java

[cc lang=”java”]

/**
* Counts the number of matching brackets: {}.
*
* @param message the string to examine
* @return the number of matching brackets
*/
private int countBrackets(final String message) {
boolean openBracket = false;
int count = 0;
for (final Character ch : message.toCharArray()) {
if (openBracket == true && ch.equals(‘}’)) {
openBracket = false;
count++;
}
if (ch.equals(‘{‘)) {
openBracket = true;
}
}
return count;
}
[/cc]

How to add icon for JComboBox items

[cc lang=”java”]
public class ShowConboWithIcons extends JFrame {

private static final long serialVersionUID = 1L;

private static final ImageIcon INFO_ICON = new ImageIcon(“lock.jpg”);
private static final ImageIcon NONE_ICON = new ImageIcon(“lock.jpg”);
public final String NONE_STR = “None”;
private final String INFO_STR = “Info”;

private JComboBox comboBox;
private JPanel topPanel;

private String[] str_arr = null;

public ShowConboWithIcons(String[] str_arr) {
this.str_arr = str_arr;
}

public void createGUI(){

setMinimumSize(new Dimension(100,100));
setTitle(“Demo”);
setLocation(200, 200);

topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);

Map icons = new HashMap();

icons.put(NONE_STR, NONE_ICON);
icons.put(INFO_STR, INFO_ICON);

comboBox = new JComboBox();
comboBox.setRenderer(new IconListRenderer(icons));
comboBox.addItem(“None”);

for(String val : str_arr){
comboBox.addItem(val);
}

topPanel.add(comboBox);

super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

UIManager.setLookAndFeel( “com.sun.java.swing.plaf.windows.WindowsLookAndFeel” );

String[] str_arr = {“A”, “B”, “C”, “D”, “E”};

ShowConboWithIcons T = new ShowConboWithIcons(str_arr);
T.createGUI();
T.setVisible(true);
}

class IconListRenderer extends DefaultListCellRenderer{
private static final long serialVersionUID = 1L;
private Map icons = null;

public IconListRenderer(Map icons){
this.icons = icons;
}

@Override
public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus)
{
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

// Get icon to use for the list item value
Icon icon = icons.get(value);

if(!value.toString().equals(NONE_STR)){
icon = icons.get(INFO_STR);
}

// Set icon to display for value
label.setIcon(icon);
return label;
}
}
}
[/cc]

RandomGaussian

[cc lang=”java”]
public final class RandomGaussian {

public static void main(String… aArgs){
RandomGaussian gaussian = new RandomGaussian();
double MEAN = 100.0f;
double VARIANCE = 5.0f;
for (int idx = 1; idx <= 10; ++idx){ log("Generated : " + gaussian.getGaussian(MEAN, VARIANCE)); } } private Random fRandom = new Random(); private double getGaussian(double aMean, double aVariance){ return aMean + fRandom.nextGaussian() * aVariance; } private static void log(Object aMsg){ System.out.println(String.valueOf(aMsg)); } } [/cc] http://www.javapractices.com/topic/TopicAction.do?Id=62

How to convert a String of Binary values to a Float and vice-versa in Java?

here’s a solution to have the binary representation of the IEEE 754 floating-point “double format” bit layout for a float (it is basically the memory representation of a float) :

int intBits = Float.floatToIntBits(yourFloat);
String binary = Integer.toBinaryString(intBits);

For the reverse procedure :

int intBits = Integer.parseInt(myString, 2);
float myFloat = Float.intBitsToFloat(longBits);