Results 1 to 10 of 10
- 08-21-2012, 02:15 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
How to check duplicate Strings inputted
Hello everyone,..i am curious to know how to check if the string i Input is doubled or duplicated, this was actually discussed on our class but i wasn't able to cope up with it because I had another following class,..can anyone help me how to do this,i really want to understand how it works before attending my class because our instructor might give us an activity involving this topic, any help would be much appreciated
Java Code:import javax.swing.DefaultListModel; import javax.swing.JOptionPane; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * frmMain.java * * Created on Aug 10, 2012, 9:11:33 AM */ /** * * @author arnenang */ public class frmMain extends javax.swing.JFrame { DefaultListModel lm=new DefaultListModel(); /** Creates new form frmMain */ public frmMain() { initComponents(); setLocation(420, 180); } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); animallist = new javax.swing.JList(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); animalTfield = new javax.swing.JTextField(); jPanel1 = new javax.swing.JPanel(); addBUtton = new javax.swing.JButton(); removeButton = new javax.swing.JButton(); sizeLabel = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); animalValue = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); animalIndex = new javax.swing.JLabel(); showButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("myLittleZoo"); animallist.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { animallistMouseClicked(evt); } }); jScrollPane1.setViewportView(animallist); jLabel1.setText("My Little Zoo"); jLabel2.setText("Type here to enter new animal"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Options")); addBUtton.setText("Add"); addBUtton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addBUttonActionPerformed(evt); } }); removeButton.setText("Remove"); removeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { removeButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(addBUtton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(removeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(addBUtton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(removeButton) .addContainerGap(37, Short.MAX_VALUE)) ); sizeLabel.setText("Click here to get size"); sizeLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); sizeLabel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { sizeLabelMouseClicked(evt); } }); jLabel4.setText("Currently you select:"); animalValue.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel6.setText("At Index:"); animalIndex.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); showButton.setText("Show animals"); showButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(showButton)) .addGap(52, 52, 52) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(animalTfield) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(54, 54, 54) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(jLabel1) .addComponent(sizeLabel) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(animalIndex, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(animalValue, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(45, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(35, 35, 35) .addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(showButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(animalTfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(13, 13, 13) .addComponent(sizeLabel) .addGap(36, 36, 36) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(animalValue, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(animalIndex, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addContainerGap(76, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void showButtonActionPerformed(java.awt.event.ActionEvent evt) { String animalArray[]={"Elephant","Pig","Lion","Tiger","Bird"}; for( int x=0; x<animalArray.length; x++){ lm.addElement(animalArray[x]); } animallist.setModel(lm); } private void sizeLabelMouseClicked(java.awt.event.MouseEvent evt) { JOptionPane.showMessageDialog(animallist,"No. of animals is " + lm.getSize()); } private void animallistMouseClicked(java.awt.event.MouseEvent evt) { animalValue.setText(animallist.getSelectedValue().toString()); animalIndex.setText(Integer.toString(animallist.getSelectedIndex())); } private void addBUttonActionPerformed(java.awt.event.ActionEvent evt) { if(!animalTfield.getText().isEmpty()){ lm.addElement(animalTfield.getText()); animalTfield.requestFocus(); animalTfield.selectAll(); animallist.setModel(lm); } else{ JOptionPane.showMessageDialog(animallist,"Please Enter a name of an animal ","Data Entry Missing",JOptionPane.QUESTION_MESSAGE); } } private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ lm.remove(animallist.getSelectedIndex()); animalTfield.setText(null); } catch(ArrayIndexOutOfBoundsException e){ JOptionPane.showMessageDialog(animallist, "Pleae select animal to remove","No selected Animal",JOptionPane.ERROR_MESSAGE); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new frmMain().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton addBUtton; private javax.swing.JLabel animalIndex; private javax.swing.JTextField animalTfield; private javax.swing.JLabel animalValue; private javax.swing.JList animallist; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton removeButton; private javax.swing.JButton showButton; private javax.swing.JLabel sizeLabel; // End of variables declaration }
- 08-21-2012, 04:08 PM #2
Re: How to check duplicate Strings inputted
Can you define what you mean by doubled or duplicated? Can you give some examples?check if the string i Input is doubled or duplicate
Is "asdasd" a String with the "asd" doubled?
or is "sss" and "sss" duplicated?If you don't understand my response, don't ignore it, ask a question.
- 08-21-2012, 04:10 PM #3
Re: How to check duplicate Strings inputted
Hi,
Us contains method before using addElement like this below.
if (!lm.contains(animalTfield.getText()))
lm.addElement(animalTfield.getText());Ramya:cool:
- 08-21-2012, 04:37 PM #4
Re: How to check duplicate Strings inputted
@RamyaSivakanth Giving code with no explanation can be confusing and not help the OP learn programming.
Can you add an explanation of what the code you posted does and why it could work to solve the OP's problem?If you don't understand my response, don't ignore it, ask a question.
- 08-21-2012, 04:43 PM #5
Re: How to check duplicate Strings inputted
contains check whether the entered string already there or not by preventing us to enter duplicates.
Ramya:cool:
- 08-21-2012, 05:05 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: How to check duplicate Strings inputted
i wanted to check if the new animal i inputted is already in stored,..if it is,i wanted to get a message that the animal is already in stored on the array
- 08-21-2012, 05:07 PM #7
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: How to check duplicate Strings inputted
let me try and figure your sample out,..ill return when i get results
- 08-21-2012, 05:14 PM #8
Re: How to check duplicate Strings inputted
Read the API doc for the class that holds the list to see if any of its methods would help.
If you don't understand my response, don't ignore it, ask a question.
- 09-02-2012, 02:29 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
- 09-02-2012, 02:30 PM #10
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Ordering Inputted Java Values
By MichaelSpame in forum New To JavaReplies: 2Last Post: 07-16-2012, 05:05 AM -
Compile Error: Duplicate Class.. (after package) but I don't have a duplicate class..
By JimmyD in forum Advanced JavaReplies: 5Last Post: 06-03-2012, 07:20 PM -
removing duplicate strings from a massive array efficiently?
By sixsigma1978 in forum New To JavaReplies: 7Last Post: 04-06-2012, 12:15 AM -
Sorting printed ArrayList of user inputted strings.
By movsesinator in forum New To JavaReplies: 3Last Post: 04-03-2010, 09:27 PM -
Help Creating A Graph From Inputted Data
By adlb1300 in forum New To JavaReplies: 6Last Post: 10-28-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks