简体   繁体   English

在字符串中查找非重复字符

[英]Find non-repeated character in a string

I have to create a program for returning the next non-repeated character..我必须创建一个程序来返回下一个非重复字符..

ex I give ... tweet前我给... tweet
and it should return output as w ...它应该将输出返回为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);
                }
            }
        }
    }
}

I am getting error as .. missing return statement..我收到错误,因为 .. 缺少 return 语句..

Can anyone please tell me.. How do I solve such a problem.. Where am I wrong..?谁能告诉我..我该如何解决这样的问题..我错在哪里..?

To solve your problem simply add,要解决您的问题,只需添加,

return d;

in your function.在你的函数中。 But it's better to understand how this actually works:但最好了解这实际上是如何工作的:

Functions/Methods are written as函数/方法写为

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

For eg例如

public  char returnChar(int a)
 |       |       |       |
 |       |       |       |
 ^       ^       ^       ^
accessor return  name   parameter

this means that this function will return a character .这意味着该函数将返回一个字符

In the sense, you need to a char like this in your function从某种意义上说,您需要在函数中使用这样的字符

return char;

Try reading up on methods and their return types.尝试阅读方法及其返回类型。 :) :)

References:参考资料:

你还没有写 return 语句。使用return ;

You have written the return type for revString(String str) as char and you are not returning any character.您已将 revString(String str) 的返回类型写为 char 并且您没有返回任何字符。 Change that return type to void or add line return d;将该返回类型更改为 void 或添加行 return d; to the method到方法

You have missing the return statement in your code.您的代码中缺少 return 语句。

here is the code which returns what you want这是返回您想要的内容的代码

CODE代码

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;

}

Use like this像这样使用

char my =  findFirstNonRepeated("twwwweety");
System.out.println(my);

This will return y .这将返回y

your program should be like this:你的程序应该是这样的:

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

Add each character to a HashSet and check whether hashset.add() returns true, if it returns false ,then remove the character from hashset.将每个字符添加到 HashSet 并检查 hashset.add() 是否返回 true ,如果返回 false ,则从 hashset 中删除该字符。 Then getting the first value of the hashset will give you the first non repeated character.然后获取哈希集的第一个值将为您提供第一个非重复字符。 Algorithm:算法:

  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.

Try this, // Split the string into characters // Check if entry exists in the HashMap, if yes- return the character, if No- inert the element with value 1试试这个, // 将字符串拆分为字符 // 检查 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';
}

IN JAVA using for loop only..... it is very easy....you can do it without collection in java.. just try it.....在 JAVA 中只使用 for 循环..... 很容易....你可以在没有 Java 集合的情况下做到...试试吧.....

public class FirstNonRepeatedString {公共类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;
    } 
    }
}

} }

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

            }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM