Java: Print a unique character in a string

前端 未结 17 1305
长情又很酷
长情又很酷 2021-01-03 00:03

I\'m writing a program that will print the unique character in a string (entered through a scanner). I\'ve created a method that tries to accomplish this but I keep getting

17条回答
  •  感情败类
    2021-01-03 00:39

    Based on your desired output you can replace each character already present with a blank character.

    public static void uniqueCharacters(String test){
      String temp = "";
      for(int i = 0; i < test.length(); i++){
          if (temp.indexOf(test.charAt(i)) == - 1){
             temp = temp + test.charAt(i);
          } else {
             temp.replace(String.valueOf(temp.charAt(i)), "");
          }
     }
    
    System.out.println(temp + " ");
    

    }

提交回复
热议问题