1

I made a program that removes duplicates of characters in a String when pressing a button. So for example if the String is "23332" the output is "23". Now my problem is that this dont really works with numbers above 10. If the String is "10" the programe sees it as 1 and 0 and not as 10. Is it possible to somehow mark that numbers so that the program know that it is 10 and not 1 and 0?. Thanks in advance. Here is my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class test extends JFrame implements ActionListener {

    private JPanel panel;
    private JLabel label;
    private JButton ok;
    private String str;

    public test() {

        panel = new JPanel();

        ok = new JButton("OK");
        ok.addActionListener(this);

        str = "10, 11, 12, 13, 10";
        label = new JLabel(str);

        panel.add(ok);
        panel.add(label);

        add(panel);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        }

    public static void main(String[] args) {
        new test();        
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        String temp="";
        HashMap<Integer,Character> tc = new HashMap<Integer,Character>();
        char [] charArray = str.toCharArray();
        for (Character c : charArray)
        {
            if (!tc.containsValue(c))
                {
                    temp=temp + c.toString() + ", ";
                    tc.put(c.hashCode(),c);
                 }
        }
        label.setText(temp);
    }
}

EDIT:

    String temp = "";
    HashMap<Integer,Character> tc = new HashMap<Integer,Character>();
    String[] strArray = str.split(",");

    for(String string : strArray) {
        char [] charArray = string.toCharArray();
            for (Character c : charArray)
            {
                if (!tc.containsValue(c))
                    {
                        temp=temp + c.toString() + ", ";
                        tc.put(c.hashCode(),c);
                     }
            }
        }
            label.setText(temp);
Duardo
  • 107
  • 11
  • Split the string on the commas (http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java), trim each block, and parse them as Int (Integer.parseInt), and keep a Set of Integer to remember which one you've seen. – JP Moresmau May 22 '15 at 16:44
  • no, how could the program possibly know if you meant "45" or "4" and "5"? The only way to solve this is by adding a separation character (like you have in the string `"10, 11, 12, 13, 10"`) and then storing each number in an `int` array instead of a `char` array. – moffeltje May 22 '15 at 16:45
  • Do you have an upper bound? – Anindya Dutta May 22 '15 at 16:46
  • 1
    Despite mention of buttons and the presence of a GUI, this is really not a problem that is related to either GUIs or the [tag:swing] tag. In fact, you could, and should, solve this type of problem in command line apps. before ever trying it in a GUI. – Andrew Thompson May 22 '15 at 16:58

2 Answers2

1

Assuming that you are using a separator such as ,, you can use String.split() method which gives you an array of Strings and loop through all this strings, here's the code you need:

String[] strArray = str.split(",");
for(String string : strArray) {
char [] charArray = string.toCharArray();
    for (Character c : charArray)
    {
        if (!tc.containsValue(c))
            {
                temp=temp + c.toString() + ", ";
                tc.put(c.hashCode(),c);
             }
    }
}

EDIT:

In the case you need to check for duplicates of numbers higher than 10, you need to use Integer.parseIn() to parse the String values and change your Map from HashMap<Integer,Character> to HashMap<Integer,String>, here's the code:

String[] strArray = str.split(",");
for(String string : strArray) {
   if (!tc.containsValue(string)) {
          temp=temp + string + ", ";
          tc.put(Integer.parseIn(string), string);
   }
}
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Create a split the string with comma . add each element to a set of strings.Now you have a set of unique numbers.(Set discards duplicate values)

Set<String> unique = new HashSet<String>();
String myString[]=str.split(",");    
for(String t:myString)
unique.add(t)
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37