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);