2014-03-29 74 views
0

我試圖從它的標點分開一個字:拆分爲兩個部分字符串如果p

因此,舉例來說,如果這個詞是"Hello?"。我想將"Hello"存儲在一個變量中,"?"存儲在另一個變量中。

這是到目前爲止我的代碼:

String inWord = "hello?"; 
    if (inWord.contains(","+"?"+"."+"!"+";")) { 

     String parts[] = inWord.split("\\," + "\\?" + "\\." + "\\!" + "\\;"); 
     String word = parts[0]; 
     String punctuation = parts[1]; 
    } else { 
     String word = inWord; 
    } 

    System.out.println(word); 
    System.out.println(punctuation); 

我的問題是,我得到的錯誤:當我無法找到符號並打印出來的字和標點。

感謝您的幫助提前

+2

你明白['+'='或'!]? –

+1

像「String word = ...」這樣的變量聲明的範圍只是它所在的塊({'和'}'中的代碼段)。變量'word'和'標點符號'存在於您嘗試打​​印它們的範圍內。 –

+0

此外,你可能更好地使用String.matches,而不是String.contains – donfuxx

回答

0

還有其他的事情你的代碼錯誤,但你的問題是,爲什麼你會得到「無法找到符號」錯誤。

String inWord = "hello?"; 
String word; 
String punctuation = null; 
if (inWord.contains(","+"?"+"."+"!"+";")) { 
    String parts[] = inWord.split("\\," + "\\?" + "\\." + "\\!" + "\\;"); 
    word = parts[0]; 
    punctuation = parts[1]; 
} else { 
    word = inWord; 
} 

System.out.println(word); 
System.out.println(punctuation); 

String word = ...一個變量聲明的範圍只有塊(內部「{」和「}」代碼片段),它在該變量wordpunctuation不在範圍存在您嘗試打印它們。

您需要聲明你在同一範圍內的變量wordpunctuation(或封閉的範圍),在那裏你訪問他們在你System.out.println

+1

好吧,代碼將立即編譯,但我猜輸出不會是預期的結果... – donfuxx

0

您可以用自定義的嘗試中包含的功能和StringTokenizer 爲:

public class Test{ 

    public static void main(String[] args) { 
     String inWord = "hello"; 
     String[] wordAndPunctuation = null; 
     char[] punctuations =new char[]{',','?','.','!',';'}; 
     StringTokenizer tokenizer = new StringTokenizer(inWord,new String(punctuations),true); 
     int i = 0; 

     if (Test.contains(inWord,punctuations)) { 
      while(tokenizer.hasMoreTokens()){ 
       wordAndPunctuation = new String[tokenizer.countTokens()]; 
       System.out.println(tokenizer.countTokens()); 
       wordAndPunctuation[i] = tokenizer.nextToken(); 
       i++; 
      } 
     }else{ 
      System.out.println("No punctuation in "+inWord); 
     } 
    } 

    public static boolean contains(String str, char[] charArr){ 
     System.out.println(Arrays.toString(charArr)); 
     for(char c:charArr){ 
      if(str.contains(String.valueOf(c))) 
      return true; 
     } 
     return false; 
    } 
} 
+0

'StringTokenizer'已棄用。請參閱[this](http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split)。 – Astrobleme

0

您在代碼中發生了以下錯誤。

1.Declare外字符串if條件

2. inWord.contains( 「 」+ + + +「? 」「。 」「! 」「;」)這等於inword.contains(「;?!」),這樣的情況將在此基礎上總是失敗,它轉到其他條件

  1. 分裂()不會存儲值你分割字符串

例如

String string = "004-034556"; 

String[] parts = string.split("-"); 

String part1 = parts[0]; // 004 

String part2 = parts[1]; // 034556 

在這個值「 - 」不能存儲。希望你明白我想傳達的。

0

我會建議通過String分析和檢查,如果字符是一個標點符號方法:

String sentence = "Hello? Is this Mrs. Doubtfire?"; // Example. 
ArrayList<String> chunks = new ArrayList<>(); // Will store the "non-punctuated chunks" 
ArrayList<Character> puncts = new ArrayList<>();// Will the punctuations in the "sentence" 
char[] punctuations = {',','?','.','!',';'}; // Store punctuations here. 
int lastIndex = 0; 
for (int i = 0; i < sentence.length(); i++) { 
    char c = sentence.charAt(i); 
    for (char punctuation : punctuations) { 
     if (c == punctuation) { 
      chunks.add(sentence.substring(lastIndex, i).trim()); 
      puncts.add(c); 
      lastIndex = i + 1; 
     } 
    } 
} 
System.out.println(chunks); 
System.out.println(puncts); 

輸出:

[Hello, Is this Mrs, Doubtfire] 
[?, ., ?] 

記得import java.util.ArrayList

0

你爲什麼不這樣做:

String s = "hello!"; 

Pattern p = Pattern.compile("(\\w+)?(\\W)"); 
Matcher m = p.matcher(s); 
while (m.find()) { 
    System.out.println("Word: " + m.group(1) + " | Punctuation: " + m.group(2)); 
} 

組別1將包含文字和第2組將包含標點符號。

演示:http://ideone.com/ljIZFW