Tại sao hàm contain lại luôn trả về false?

Mọi người cho em hỏi tại sao hàm contain này của em lại luôn trả về false

package gui;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;

import input.Stock_Record;
import solution.*;

public class GUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JLabel txtSecurities;
	private JList<String> list;
	private JTextArea textArea;
	private DefaultListModel<String> listModel;
	
	private Key key=new Key();
	private static ArrayList<Stock_Record> dsCK;
	private String[] data;
	private String[] symbol=key.listSymbol(dsCK);
	private String[] listKey=key.dSKhoa();
	public static void setDsCK(ArrayList<Stock_Record> dsCK) {
		GUI.dsCK = dsCK;
	}

	public static ArrayList<Stock_Record> getDsCK() {
		return dsCK;
	}
	public static boolean contain(String s,String[] arr) {
		boolean flag=false;
		for(int i=0;i<arr.length;i++) {
			try {
				if(arr[i].toLowerCase().contains(s.toLowerCase())) 
					flag=true;
			} catch (Exception e) {
			}
		}
	return flag;
}

	/**
	 * Create the frame.
	 */
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 599);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setForeground(Color.WHITE);
		setTitle("Thông tin chứng khoán");
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Search:");
		lblNewLabel.setBounds(10, 186, 72, 35);
		lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		getContentPane().add(lblNewLabel);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Toàn bộ", "Thông tin mã cổ phiếu", "Thông tin giao dịch cổ phiếu"}));
		comboBox.setBounds(79, 186, 307, 31);
		contentPane.add(comboBox);
		comboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedIndex()==2)
					data=listKey;
				else if(comboBox.getSelectedIndex()==1)
					data=symbol;
				else {
					data = Arrays.copyOf(symbol, symbol.length + listKey.length);
					  System.arraycopy(listKey, 0, data, symbol.length, listKey.length);
				}		
			}
		});

		JScrollPane scroll = new JScrollPane(list);
		scroll.setLocation(79, 254);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setPreferredSize(new Dimension(250,250));
		scroll.setSize( 450, 119 );
		listModel= new DefaultListModel<String>();
		list= new JList<String>();
		list.setModel(listModel);
		list.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		list.setBounds(79,222,449,100);
		list.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				textField.setText(list.getSelectedValue());
			}
			
		});
		
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setLocation(79, 254);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(250,250));
		scrollPane.setSize( 450, 119 );
		list.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				scrollPane.setVisible(false);
				
			}
		});
		
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			 @Override
			public void keyReleased(KeyEvent e) {
					String string=textField.getText().toLowerCase();
					 int length=string.length();
					 if(length==0) 
						listModel.clear();
					 else
					 {
						 try {
					 for(String i:data) {
						 if(i.toLowerCase().contains(string))
							 {
							 if(listModel.contains(i))
								 continue;
							 if(!listModel.contains(i))
								 listModel.addElement(i);
							 
							 }
						 else listModel.removeElement(i);
					 }
						 }
						catch (Exception e1) {
						}
				 }
					 }
			
		});
		 textField.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent e) {
				try {
					getContentPane().add(scrollPane);
					scrollPane.setVisible(false);
					if(e.getMark()>0)
						scrollPane.setVisible(true);
				}
				catch (Exception e1) {
				}
				
			}
		});
		textField.setForeground(new Color(0, 0, 0));
		textField.setBounds(79, 219, 535, 35);
		textField.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		getContentPane().add(textField);
		
		JButton btn = new JButton();
		btn.setBounds(539, 265, 76, 59);
		btn.setIcon(new ImageIcon("C:\\Users\\Dell\\Downloads\\q (1).png"));
		btn.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		btn.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				boolean flag=contain(textField.getSelectedText(), data);
				
					if(flag==true) {
							boolean f=contain(textField.getSelectedText(), listKey);
						if(f==true)
							byKey();
						else bySymbol();
					}
					else  {
						textArea.setText("Xin lỗi, thông tin mà bạn tìm kiếm không tồn tại."+"\n"
								+"Hãy thử tìm kiếm thông tin khác.");
}
						
				}
			
		});
		getContentPane().add(btn);
		

		txtSecurities = new JLabel();
		txtSecurities.setForeground(new Color(30, 144, 255));
		txtSecurities.setFont(new Font("Lucida Calligraphy", Font.PLAIN, 80));
		txtSecurities.setText("Securities");
		txtSecurities.setBounds(100, 0, 415, 157);
		getContentPane().add(txtSecurities);
		
		JLabel lblNewLabel_1 = new JLabel("Stock information on 15/05/2020");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(248, 132, 244, 23);
		getContentPane().add(lblNewLabel_1);
		
		JButton btn_1 = new JButton("Refresh");
		btn_1.setFont(new Font("Times New Roman", Font.PLAIN, 15));
		btn_1.setBackground(new Color(255, 255, 255));
		btn_1.setForeground(new Color(0, 0, 0));
		btn_1.setBounds(535, 335, 79, 38);
		btn_1.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				textField.setText(null);
				listModel.clear();
				textArea.setText(null);
			}
			
		});
		getContentPane().add(btn_1);
			
			textArea=new JTextArea(5,20);
			contentPane.add(textArea);
			textArea.setFont(new Font("Times New Roman", Font.PLAIN, 18));
			textArea.setWrapStyleWord(true);
			textArea.setLineWrap(true);
			textArea.setEditable(false);
			textArea.setBounds(79,339,496,109);
			
			JScrollPane areaScroll=new JScrollPane(textArea);
			areaScroll.setLocation(40, 384);
			areaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			areaScroll.setPreferredSize(new Dimension(250,250));
			areaScroll.setSize( 574, 157 );
			getContentPane().add(areaScroll);
			
			
			}
	public void byKey() {
		textArea.setText(key.searchBy(list.getSelectedValue()));
	}
	public void bySymbol() {
		
			String[] str=key.searchBySymbol(textField.getSelectedText(), dsCK);
			String line = "";
			try{
				for(int j=0;j<str.length;j++)
			
				line += str[j] + "\n ";}
			catch (Exception e) {
				
			}
			finally {
				textArea.setText(line);
			}
				
	}
	
}

bạn kiểm tra lại dữ liệu chưa

2 Likes

Không phải luôn trả về false đâu, nó vẫn trả về true đấy. Khi phần tử cuối cùng của mảng giống phần tử cần xét, sẽ trả về true. Thử đi.
Tìm kiếm phần tử mà dùng cờ (flag) bảo sao sai.

2 Likes

em vừa mới học java. Cho nên anh cho em hỏi sửa như thế nào để nó trả về true hoặc false ko ạ?

Xem chưa kĩ, phương thức contain() của bạn vẫn đúng. Nhưng dùng flag là thừa.

  1. Không cần flag. Nếu chứa chuỗi thì trả về true ngay lập tức. Nếu đã duyệt xét hết mảng mà không tìm thấy, trả về false.
  2. Xem lại mảng/chuỗi cần xét, có bị null hay không. Có đúng các phần tử mà bạn mong muốn không hay toàn là chuỗi rỗng?
  3. Phần try - catch bạn bắt, nhưng không ghi nhận ngoại lệ, nếu thực sự có ngoại lệ xảy ra thì sao biết nó bị gì mà khắc phục? Ghi nhận ở đây là in ra Console hoặc làm gì đó để khi chạy biết rằng lúc đó có ngoại lệ xảy ra.

Bạn đang muốn tìm toàn bộ văn bản HAY văn bản được chọn trong textField? Vì thấy bạn gọi: textField.getSelectedText() (văn bản được chọn). Nếu bạn không chọn gì thì chuỗi cần tìm = "" (chuỗi trống).

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?