Results 1 to 10 of 10
  1. #1
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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                   
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: How to check duplicate Strings inputted

    check if the string i Input is doubled or duplicate
    Can you define what you mean by doubled or duplicated? Can you give some examples?

    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.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default 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:

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default 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:

  6. #6
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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

  7. #7
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to check duplicate Strings inputted

    let me try and figure your sample out,..ill return when i get results

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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.

  9. #9
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to check duplicate Strings inputted

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    Us contains method before using addElement like this below.
    if (!lm.contains(animalTfield.getText()))

    lm.addElement(animalTfield.getText());
    thanks a lot for your help,this really helped a lot :)

  10. #10
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to check duplicate Strings inputted

    thank you so much for taking your time in helping me :) figured it out from the reply below your reply

Similar Threads

  1. Ordering Inputted Java Values
    By MichaelSpame in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 05:05 AM
  2. Replies: 5
    Last Post: 06-03-2012, 07:20 PM
  3. Replies: 7
    Last Post: 04-06-2012, 12:15 AM
  4. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 09:27 PM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •