Reputation: 41
I dont know whts happening.THe code inside if block is running even though the condition is false.I have designed my interface in netbeans ide 7.4 and just copy pasted the design in eclipse.Here is my code
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if");
}
}
});
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
whatever i select inside the jinstrument jcombobox i get "inside if " as the output.I have checked through print statements but enterloop is having value 0 and it still enters the if statement block.Please help me with this problem
Upvotes: 0
Views: 155
Reputation: 201537
Your code has a fatal defect, namely one
if(enterloop == 1) // ; bad semi-colon. Which, made this a single line if.
{ // And, as a result this was "just" an anonymous code block.
System.out.println("inside if");
}
Upvotes: 0
Reputation: 1582
try these updated codes::
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if")
}
}
});
instrumentLabel.setText("Instrument");
jPanel2.add(instrumentLabel);
instrumentLabel.setBounds(21, 11, 102, 14);
symbolLabel.setText("Symbol");
jPanel2.add(symbolLabel);
symbolLabel.setBounds(129, 11, 126, 14);
expiryLabel.setText("Expiry");
jPanel2.add(expiryLabel);
expiryLabel.setBounds(261, 11, 64, 14);
optionLabel.setText("OptionType");
jPanel2.add(optionLabel);
optionLabel.setBounds(377, 11, 56, 14);
strikeLabel.setText("Strike Price");
jPanel2.add(strikeLabel);
strikeLabel.setBounds(462, 11, 53, 14);
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
your code is giving error because you have a ";" as
if(enterloop == 1);
{ System.out.println("inside if")
}
there should not be a termination after the if condition. If there is a semicolon(termination) after your condition then whatever codes you write inside{ //the curly braces} will be never executed.
Upvotes: 0
Reputation: 13854
there is a ;
in this line if(enterloop == 1);
;
means end of statement.So even the if statement returns false so the next line will be executed
Upvotes: 6