Java 在字符串中查找不重复的字符

Java 在字符串中查找不重复的字符,java,string,Java,String,我必须创建一个程序来返回下一个非重复字符 我给推特 它应该以w的形式返回输出 public class str_next { public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the stri

我必须创建一个程序来返回下一个非重复字符

我给<代码>推特
它应该以
w
的形式返回输出

public class str_next {

    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the string");
        String s = br.readLine();
        revString(s);
    }

    static char revString(String str) {
        int i = 0;
        int j;
        int n = str.length();
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
                char c = str.charAt(i);
                char d = str.charAt(j);
                if (c != d) {
                    System.out.print(d);
                }
            }
        }
    }
}
公共类stru\u下一步{
公共静态void main(字符串args[])引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入字符串”);
字符串s=br.readLine();
revString(s);
}
静态字符revString(字符串str){
int i=0;
int j;
int n=str.length();
对于(i=0;i
我得到的错误是。。缺少返回语句


谁能告诉我。。我如何解决这样的问题。。我错在哪里?

您还没有编写return语句。请使用
return

您已经将revString(String str)的返回类型写为char,并且没有返回任何字符。 将该返回类型更改为void
或添加行返回d;对于解决问题的方法,只需添加

return d;
在你的职责范围内。但更好的办法是了解这实际上是如何工作的:

函数/方法写为

accessor_type return_type function_name(parameter_list)
{
 //stuff to do in your code
}
例如

public  char returnChar(int a)
 |       |       |       |
 |       |       |       |
 ^       ^       ^       ^
accessor return  name   parameter
这意味着此函数将返回一个字符

从这个意义上讲,您需要在函数中添加这样的字符

return char;
尝试阅读方法及其返回类型。:)

参考资料:


代码中缺少return语句

下面是返回所需内容的代码

代码

public static Character findFirstNonRepeated(String input) {
    // create a new hashtable:
    Hashtable<Character, Object> hashChar = new Hashtable<Character, Object>();

    int j, strLength;
    Character chr;
    Object oneTime = new Object();
    Object twoTimes = new Object();

    strLength = input.length();

    for (j = 0; j < strLength; j++) {
        chr = new Character(input.charAt(j));
        Object o = hashChar.get(chr);

        /*
         * if there is no entry for that particular character, then insert
         * the oneTime flag:
         */
        if (o == null) {
            hashChar.put(chr, oneTime);
        }
        /*

  */
        else if (o == oneTime) {
            hashChar.put(chr, twoTimes);
        }
    }

    /*
     * go through hashtable and search for the first nonrepeated char:
     */

    int length = strLength;
    for (j = 0; j < length; j++) {
        chr = new Character(input.charAt(j));
        Object c = null;
        if (hashChar.get(chr) == oneTime)
            return chr;
    }
    /*
     * this only returns null if the loop above doesn't find a nonrepeated
     * character in the hashtable
     */
    return null;

}

这将返回
y

您的程序应该是这样的:

import java.io.*;

public class str_next {

    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the string");
        String s = br.readLine();
        revString(s);
    }

    static char revString(String str) {
        int i = 0;
        int j;
        int n = str.length();
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
                char c = str.charAt(i);
                char d = str.charAt(j);
                if (c != d) {
                    System.out.print(d);
                }
            }
        }
        return 0;
    }
}
import java.io.*;
公共类str_next{
公共静态void main(字符串args[])引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入字符串”);
字符串s=br.readLine();
revString(s);
}
静态字符revString(字符串str){
int i=0;
int j;
int n=str.length();
对于(i=0;i
将每个字符添加到哈希集中,并检查HashSet.Add()是否返回true,如果返回false,则从哈希集中删除该字符。然后获取hashset的第一个值将得到第一个非重复字符。 算法:

  for(i=0;i<str.length;i++)
  {
    HashSet hashSet=new HashSet<>()
     if(!hashSet.add(str[i))
         hashSet.remove(str[i])
    }
  hashset.get(0) will give the non repeated character.
对于(i=0;i试试这个,
//将字符串拆分为个字符
//检查HashMap中是否存在条目,如果是-返回字符,如果否-使用值1禁用元素

public static void main(String[] args) {
    String s = "rep e atit";
    char c = nonRepeat(s);
    System.out.println("The first non repeated character is:" + c);
}

private static char nonRepeat(String ss) {
    char c;
    HashMap<Character, Integer> hm = new HashMap<Character, Integer>();

    for (int i = 0; i < ss.length(); i++) {
        c = ss.charAt(i); // get each chaar from string
        if (hm.containsKey(c)) {// char is already in map, increase count
            hm.put(c, hm.get(c) + 1);
            return c;
        } else {
            hm.put(c, 1);
        }

    }

    return '0';
}
publicstaticvoidmain(字符串[]args){
字符串s=“rep e atit”;
字符c=不重复;
System.out.println(“第一个非重复字符是:“+c”);
}
私有静态字符不重复(字符串ss){
字符c;
HashMap hm=新的HashMap();
对于(int i=0;i
仅用于循环。。。。。 这是非常简单的…在java中不需要收集就可以做到。。 试试看

公共类FirstNonRepeatedString{

public static void main(String args[]) {


    String input = "tweet";
    char process[] = input.toCharArray();
    boolean status = false;
    int index = 0;
    for (int i = 0; i < process.length; i++) {
        for (int j = 0; j < process.length; j++) {

            if (i == j) {
                continue;
            } else {
                if (process[i] == process[j]) {
                    status = false;
                    break;
                } else {
                    status = true;
                    index = i;
                }
            }

        }
         if (status) {
        System.out.println("First non-repeated string is : " + process[index] + " INDEX " + index);
        break;
    } 
    }
}
publicstaticvoidmain(字符串参数[]){
字符串输入=“tweet”;
char进程[]=input.toCharArray();
布尔状态=假;
int指数=0;
for(int i=0;i
}

公共课堂实践{
公共静态void main(字符串参数[])
{
System.out.println(“输入字符串”);
扫描仪s=新的扫描仪(System.in);
字符串输入=s.nextLine();
int length=input.length();

对于(int i=0;i)添加缺少的返回语句。非重复单词或非重复字母您打印的是字符,而不是返回字符。两者不一样。有什么区别?为什么?复制粘贴解决方案解决了此问题,但从长远来看没有多大好处。
public static void main(String args[]) {


    String input = "tweet";
    char process[] = input.toCharArray();
    boolean status = false;
    int index = 0;
    for (int i = 0; i < process.length; i++) {
        for (int j = 0; j < process.length; j++) {

            if (i == j) {
                continue;
            } else {
                if (process[i] == process[j]) {
                    status = false;
                    break;
                } else {
                    status = true;
                    index = i;
                }
            }

        }
         if (status) {
        System.out.println("First non-repeated string is : " + process[index] + " INDEX " + index);
        break;
    } 
    }
}
public class JavaPractice {

public static void main(String args[])
    {
System.out.println("Enter input String");
        Scanner s= new Scanner(System.in);
        String input= s.nextLine();
        int length=input.length();
        for(int i=0;i<length;i++)
        {
            int temp=0;
            for(int j=0;j<length;j++)
            {
                if(input.charAt(i)==input.charAt(j))
                {temp++;}
            }
            if(temp==1)
            {System.out.println(input.charAt(i));}

            }
}
}