如何使用方法在java中查找公共后缀

时间:2015-11-21 03:40:49

标签: java string methods suffix

如何使用方法

在java中查找公共后缀

public static String commonSuffix (String s1, String s2)

我无法在方法中返回结果。请帮帮我

import java.util.Scanner;

public class ders1 {

    public static void main(String[] args) {
        //HW3 Topic 3
        Scanner input = new Scanner(System.in);

        String reverse1="";
        String reverse2="";
     System.out.println("Please enter the first string: ");
     String s1=input.nextLine();

     System.out.println("Please enter the second string: ");
     String s2=input.nextLine();

     int l1=reverse1.length();
     int l2=reverse2.length();



     for ( int i = s1.length() - 1 ; i >= 0 ; i-- )
     {
        reverse1 = reverse1 + s1.charAt(i);
     }

     for ( int i = s2.length() - 1 ; i >= 0 ; i-- )
     {
        reverse2 = reverse2 + s2.charAt(i);
     }
     if(l1<l2){ 
        int l3=l2;

     System.out.println(reverse1 + " " + reverse2);
     System.out.println(commonSuffix(reverse1,reverse2,l3));
     }

     else {
         int l3=l1;
         System.out.println(reverse1 + " " + reverse2);

         System.out.println(commonSuffix(reverse1,reverse2,l3)); 
     }
    } 
    public static String commonSuffix (String reverse1, String reverse2,int l3){

        String suffixies="";
        for(int k=0; k<=l3 ; k++){
            if(reverse1.charAt(k)!=reverse2.charAt(k)){

        }
            else{
            suffixies+=reverse1.charAt(k);  
        }    
    }
        return suffixies;
   }
}       

有人可以帮我修复这段代码吗?

3 个答案:

答案 0 :(得分:3)

您的问题是您返回for循环内部。你应该在for循环结束后返回。

答案 1 :(得分:1)

请参阅以下代码(我已测试过):

import java.util.Scanner;

public class ders1 {

    public static void main(String[] args) {
        // HW3 Topic 3
        Scanner input = new Scanner(System.in);

        String reverse1 = "";
        String reverse2 = "";
        System.out.println("Please enter the first string: ");
        String s1 = input.nextLine();

        System.out.println("Please enter the second string: ");
        String s2 = input.nextLine();

        for (int i = s1.length() - 1; i >= 0; i--) {
            reverse1 = reverse1 + s1.charAt(i);
        }

        for (int i = s2.length() - 1; i >= 0; i--) {
            reverse2 = reverse2 + s2.charAt(i);
        }

        int l1 = reverse1.length();
        int l2 = reverse2.length();

        if (l1 < l2) {
            int l3 = l1;
            System.out.println(reverse1 + " " + reverse2);
            System.out.println(commonSuffix(reverse1, reverse2, l3));
        } else {
            int l3 = l2;
            System.out.println(reverse1 + " " + reverse2);
            System.out.println(commonSuffix(reverse1, reverse2, l3));
        }
    }

    public static String commonSuffix(String reverse1, String reverse2, int l3) {
        String suffixies = "";
        for (int k = 0; k < l3; k++) {
            if (reverse1.charAt(k) != reverse2.charAt(k)) {
                break;
            } else {
                suffixies += reverse1.charAt(k);
            }
        }

        // Reverse again
        String reverse = "";
        for (int i = suffixies.length() - 1; i >= 0; i--) {
            reverse = reverse + suffixies.charAt(i);
        }

        return reverse;
    }
}

输出:

Please enter the first string: 
caption
Please enter the second string: 
action
noitpac noitca
tion

答案 2 :(得分:0)

这可能是最好的

** 包com.tm;

导入java.util.Scanner;

公共类CommSuffix {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String str[] = scan.nextLine().split(",");

    String commSufffix=null;
    if(str[0].trim().length() > str[1].trim().length()) {
            String temp = str[0].trim();
            str[0] = str[1].trim();
            str[1] = temp;
    }

    for(int i=0;i<str[0].length();i++) {
        String subStr = str[0].substring(i,str[0].length());
        if(str[1].endsWith(subStr)) {
            commSufffix = subStr;
            break;
        }
    }

    System.out.println(commSufffix);
}

} **