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

时间:2013-10-10 04:43:02

标签: java string

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

前我给... tweet
它应该将输出返回为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);
                }
            }
        }
    }
}

我收到错误,因为...缺少退货声明..

任何人都可以告诉我..我如何解决这样的问题..我哪里错了..?

8 个答案:

答案 0 :(得分:1)

要解决您的问题,只需添加

即可
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;

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

参考文献:

答案 1 :(得分:0)

您尚未撰写return语句。请使用return ;

答案 2 :(得分:0)

您已将revString(String str)的返回类型写为char,并且您没有返回任何字符。 将返回类型更改为void 或添加行返回d;方法

答案 3 :(得分:0)

您的代码中缺少return语句。

这里是返回你想要的代码

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

}

像这样使用

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

这将返回y

答案 4 :(得分:0)

你的程序应该是这样的:

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

}

答案 5 :(得分:0)

试试这个, //将字符串拆分为字符 //检查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';
}

答案 6 :(得分:0)

IN JAVA 仅用于循环..... 这很容易....你可以在没有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;
    } 
    }
}

}

答案 7 :(得分:0)

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

            }
}
}