2017-05-30 65 views
-1

必須將字符串作爲單個單詞,以及如何比較和提取字符串或字符串以顯示差異?如何解析和比較文本?

也許可能有不同的解決方案讓它工作吧。

代碼

package parsetest; 

import java.util.StringTokenizer; 

public class ParseTest { 
String saR1 = "This is a test for checking the content and a test for compare it"; 
String saR2 = "This is a test to the content and a test to make a comparsion"; 
String sResult1 = ""; 
String sResult2 = ""; 
String differences = ""; 

public static void main(String[] args) { 
    new ParseTest().parseMethod(); 
} 

private void parseMethod() { 
System.out.println("This is stringR1 :" + saR1); 
System.out.println("This is stringR2 :" + saR2); 
StringTokenizer st1 = new StringTokenizer(saR1); // do I need a StringTokenizer ? 
StringTokenizer st2 = new StringTokenizer(saR2); 
if(!saR1.equals(saR2)) { 
    while(st1.hasMoreTokens()) { // like below 
    sResult1 = st1.nextToken(); 
    System.out.println("This is Result1 :" + sResult1); 
    } 
    while(st2.hasMoreTokens()) { // checks if there are more strings 
    sResult2 = st2.nextToken(); /next string 
    System.out.println("This is Result2 :" + sResult2); 
    if(sResult1.equals(sResult2)) { // when the strings are equal 
    differences = sResult1.replace(sResult2, ""); // extract the differences ? 
    } 
    } System.out.println("This is the difference :" + differences); 
} 
}} 
+0

You're已經與'bw.readLine()'讀他們,可以用'平等copare他們'''''''和其他一些'字符串'方法,但是你不清楚你真的在爲我打開什麼。 – SomeJavaGuy

+0

請把真實[mcve];包括您正在使用的確切輸入的描述;預期和實際行爲。 – GhostCat

+0

是的,對不起,我太過分了。我如何顯示字符串之間的差異,我需要StringTokenizer嗎? –

回答

0

一個可能的解決方案,我發現工作:

package parsetest; 

import java.util.ArrayList; 

public class ParseTest { 
String saR1 = "This is a Test for checking the content and a Test to compare it"; 
String saR2 = "This is the second Test for checking the seconds content and a second Test to compare it"; 
String diff1 = ""; 
String diff2 = ""; 

public static void main(String[] args) { 
    new ParseTest().parseMethod(); 
} 

private void parseMethod() { 
    String[] sa1 = saR1.split("\\s"); // split into single words 
    String[] sa2 = saR2.split("\\s"); 

    ArrayList<String> al1 = new ArrayList<String>(); // create ArrayList with more methods to manipulate, avaiable from the api 
    ArrayList<String> al2 = new ArrayList<String>(); 

for(int o = 0; o<sa1.length; o++) { // adding single elements of array[] to ArrayList 
    al1.add(sa1[o]); 
} 
for(int o = 0; o<sa2.length; o++) { 
    al2.add(sa2[o]); 
} 

for(int oi = 0; oi<al2.size()+al1.size(); oi++) { // walking thru the text, and extract the differences 
    for(int o4 = 0; o4<al2.size(); o4++) { 
    for(int o3 = 0; o3<al1.size(); o3++) { 
    if(al2.get(o4).equalsIgnoreCase(al1.get(o3))) { 
    al2.remove(al2.get(o4)); // remove equal elements 
    al1.remove(al1.get(o3)); 
    } 
    } 
    } 
} 

for(String or1 : al1) { // walking thru the arraylists with remaining elements and printing out results 
    diff1 += " " + or1; 
} System.out.println("This is saR1 :" + saR1); 
    System.out.println("This is the difference in saR1 :" + diff1); 
for(String or2 : al2) { 
    diff2 += " " + or2; 
} System.out.println("This is saR2 :" + saR2); 
    System.out.println("This is the difference in saR2 :" + diff2); 
}}