I am writing a program to display only the unique characters in a string which is entered by the user through a Scanner
.
For example, if the user enters the following line
eleven seven
Then my expected output will be
lvn svn
Here's my code:
import java.util.Arrays;
import java.util.Scanner;
public class unique_element {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String str = sc.nextLine();
char value = 0;
String str1[] = str.split(" ");
for (int k=0;k<str1.length;k++){
char string[] = str1[k].toLowerCase().toCharArray();
String temp = "";
for(int i=0;i<string.length;i++){
char current = string[i];
if(temp.indexOf(current)<0){
temp = temp + current;
}else{
temp = temp.replace(String.valueOf(current), "");
}
}
System.out.print(temp+" ");
}
}
}
And here's sample output with the above code:
Eleven seven
lven svn