寻找常见字符java

时间:2015-03-29 14:21:28

标签: java

我的代码出现问题,我无法弄明白为什么? 我想比较字符串以找到常见字符。我错过了第一个字母的第一个字符。

 package findEquals;

import java.io.*;

class charequals {

    public String name1, name2;
    int l1, l2, m;

    void getInputs() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out
                    .println("Enter First Name(Enter \"1\" for \"AMITABH BACHCHAN\")\n");

            name1 = br.readLine();
            if (name1.equals("1"))
                name1 = "AMITABH BACHCHAN";
            System.out
                    .println("Enter Second Name(Enter \"1\" for \"RAJNIKANTH\")\n");

            name2 = br.readLine();
            if (name2.equals("1"))
                name2 = "RAJNIKANTH";
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    void findEquals() {
        getInputs();
        l1 = name1.length();
        l2 = name2.length();
        for (int i = 0; i < l1; i++) {
            for (int j = 0; j < i; j++) {
                if (name1.charAt(i) == name1.charAt(j)) {
                    m = 0;
                    break;
                }
            }
            if (m == 0) {
                m = 1;
                continue;
            }
            for (int k = 0; k < l2; k++) {
                if (name2.charAt(k) == name1.charAt(i)) {
                    System.out.println(name1.charAt(i) + " ");
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        charequals a = new charequals();
        a.findEquals();
    }
}

OUTPUT(默认值):I T H N

它应该是:A I T H N. 为什么我错过了第一个字母?

1 个答案:

答案 0 :(得分:1)

这就是你需要的:

import java.io.*;

class charequals {

    public String name1, name2;
    int l1, l2, m;

    void getInputs() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out
                    .println("Enter First Name(Enter \"1\" for \"AMITABH BACHCHAN\")\n");

            name1 = br.readLine();
            if (name1.equals("1"))
                name1 = "AMITABH BACHCHAN";
            System.out
                    .println("Enter Second Name(Enter \"1\" for \"RAJNIKANTH\")\n");

            name2 = br.readLine();
            if (name2.equals("1"))
                name2 = "RAJNIKANTH";
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    void findEquals() {
        getInputs();
        l1 = name1.length();
        l2 = name2.length();
        for (int i = 0; i < l1; i++) {
           for (int j = 0; j < l2; j++) {
                if ( name1.charAt(i) == name2.charAt(j) ) {
                    System.out.print(name2.charAt(j) + " " );
                    char[] chars = name2.toCharArray();
                    chars[j] = '&';
                    name2 = new String(chars);
                    break;
                }
           }
        }
    }

    public static void main(String[] args) {
        charequals a = new charequals();
        a.findEquals();
    }
}

更改在findEquals()中,我对你的代码非常了解,但我猜这可以帮助你解决问题。 This work on ideone