0

why cant i get a "match" output after pressing the delete button. after entering sago as my food then tried deleting it by entering sago again in the second textfield i would still get the "not match" result... please check the image for more details.

enter image description here

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.Choice;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.AbstractListModel;

import java.awt.Component;
import java.awt.List;
import java.awt.Label;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JTextArea;


public class dast {

private JFrame frame;
private JTextField textField;

int index = 0;                                      //index or position of food and quantity
String[] foodArray = new String[99];                                    //array for food
String[] quantityArray = new String[99];                                //array for the quantity of food
private JTextField textField2;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                dast window = new dast();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public dast() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 433, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    final JComboBox comboBox2 = new JComboBox();
    comboBox2.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
    comboBox2.setBounds(64, 216, 46, 20);
    frame.getContentPane().add(comboBox2);

    JLabel lblNewLabel = new JLabel("Food:");
    lblNewLabel.setBounds(10, 188, 46, 14);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Quantity:");
    lblNewLabel_1.setBounds(10, 219, 54, 14);
    frame.getContentPane().add(lblNewLabel_1);

    final List cartList = new List();
    cartList.setBounds(191, 185, 111, 104);
    frame.getContentPane().add(cartList);

    Label label = new Label("Cart:");
    label.setBounds(191, 157, 62, 22);
    frame.getContentPane().add(label);

    final List quantityList = new List();
    quantityList.setBounds(309, 185, 26, 104);
    frame.getContentPane().add(quantityList);

    JButton btnNewButton = new JButton("Add to cart");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            foodArray[index] = textField.getText();
            quantityArray[index] = (String) comboBox2.getSelectedItem();
            index++;

            cartList.add(textField.getText());      //for adding items from textField to cartList
            quantityList.add((String) comboBox2.getSelectedItem()); //for adding items from combobox2 to quantityList

        }
    });
    btnNewButton.setBounds(64, 247, 121, 23);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_2 = new JButton("Delete");
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //String choice, choice2;
            //choice = textField2.getText();
            /*cartList.remove(index-1);
            index--;*/
            for(int i = 0 ; i < cartList.getItemCount() ; i++){
                System.out.println(textField2.getText() + "--" + cartList.getItem(i));
                if(textField2.getText() == cartList.getItem(i)){
                    System.out.println("match");
                }
                else{
                    System.out.println("not match");
                }
            }
            /*choice = (String) comboBox3.getSelectedItem();
            for(int i = 0 ; i < cartList.getItemCount() ; i++){

                if(choice == cartList.getItem(i)){
                    cartList.remove(i);
                    quantityList.remove(i);
                    comboBox3.removeItemAt(i);

                }
            }*/
        }
    });
    btnNewButton_2.setBounds(332, 295, 71, 23);
    frame.getContentPane().add(btnNewButton_2);

    List list = new List();
    list.setBounds(341, 185, 62, 104);
    frame.getContentPane().add(list);

    Label label_1 = new Label("Qty:");
    label_1.setBounds(309, 157, 26, 22);
    frame.getContentPane().add(label_1);

    Label label_2 = new Label("Price:");
    label_2.setBounds(341, 157, 54, 22);
    frame.getContentPane().add(label_2);

    JButton btnOrder = new JButton("Done");
    btnOrder.setBounds(10, 296, 121, 54);
    frame.getContentPane().add(btnOrder);

    textField = new JTextField();
    textField.setBounds(64, 185, 121, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField2 = new JTextField();
    textField2.setBounds(216, 328, 86, 20);
    frame.getContentPane().add(textField2);
    textField2.setColumns(10);
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

0

very simple example is this a very basic.

public class CoparingStrings {
    String first = "First";
    String second = "first";

    public void comparewithoprator(){
        if (first == second) {
            System.out.println("Matching");
        }else{
            System.out.println("Does not match");

        }


    }
    public void comparetousing(){
        if (first.equalsIgnoreCase(second)) {
            System.out.println("Matching");
        }else{
            System.out.println("Does not match");

        }


    }


    public static void main(String args[]){
        CoparingStrings coparingStrings = new CoparingStrings();
        coparingStrings.comparetousing();
        coparingStrings.comparewithoprator();

    }
}
Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68