Friday, August 05, 2011

Using String Tokenizer to Parse a String containing Item Name Price and Quantity to find Bill using netbeans and java

import javax.swing.DefaultListModel;

import java.util.StringTokenizer;

public class ItemEx extends javax.swing.JFrame {

static float amt=0;
/** Creates new form ItemEx */
public ItemEx() {
initComponents();
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String str=tf1.getText()+ " " + tf2.getText() + " " + tf3.getText();
DefaultListModel dlm=(DefaultListModel) list.getModel();
dlm.addElement(str);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String str1,str2,str="",dummy="";
float amt=0;
for(int i=0;i {
str=(String)list.getModel().getElementAt(i);

StringTokenizer st=new StringTokenizer(str," ");
while(st.hasMoreTokens())
{
dummy=st.nextToken();
str1=st.nextToken();
str2=st.nextToken();
amt+=Float.parseFloat(str1) * Float.parseFloat(str2);
}
}
lblmsg.setText("" + amt);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ItemEx().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblmsg;
private javax.swing.JList list;
private javax.swing.JTextField tf1;
private javax.swing.JTextField tf2;
private javax.swing.JTextField tf3;
// End of variables declaration

}

No comments:

Search

Google