2014-01-26 106 views
0

我試圖忽略與第一個字母大寫的單詞,並將其他單詞添加到List。但不幸的是,它確實沒有刪除任何內容,所有單詞都被添加到List中。這裏有什麼問題?刪除單詞如果第一個字母是大寫

import java.io.*; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.util.List; 

public class Main { 

    private char[] capLetters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 
    private StringBuffer strBuffer = new StringBuffer(""); 
    private List <String> wordList= new ArrayList(); 

    public Main() 
    { 
     File f =new File("C:/xxx/COMMON.txt"); 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(f)); 
      String str = ""; 
      int number =0; 

      while((str=br.readLine())!=null) 
      { 

       //Remove the words with first letter capital 
       for(int i=0;i<capLetters.length;i++) 
       { 
        if(str.charAt(0)==capLetters[i]) 
        { 

        } 
        else 
        { 
         wordList.add(str); 
         break; 
        } 
       }    


       number++; 

      } 

      System.out.println(number); 

      for(int i=0;i<wordList.size();i++) 
      { 
       System.out.println(wordList.get(i)); 
      } 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     System.out.println("Netbeans Version"); 
     new Main(); 
    } 


} 

回答

1

你可以這樣做

while((str=br.readLine())!=null) 
{ 
    if(!Character.isUpperCase(str.charAt(0))) 
    { 
      wordList.add(str); 
    } 
    number++; 
} 
+0

這個工作。謝謝:) –

+0

@GloryOfSuccess不客氣。 – Prabhakaran

1

你只正在檢查,如果第一個字母是capLetters[i],其中i從0開始。如果字串比「A」別的東西開始,它會去else。你應該做這樣的事情:

for(int i=0;i<capLetters.length;i++) { 
    if(str.charAt(0) == capLetters[i]) { 
     capitalFound = true; 
    } 
} 
if(!capitalFound) { 
    //add to list 
} 

當然不要忘了初始化capitalFoundfalse,並再次獲得下一個單詞之前將其設置爲false

1

代替維護capLetters並將第一個字符與其中的每個字符進行比較,可以直接使用Character.isUpperCase(char)方法。

對於當前的方法,只需將第一個字符與第一個字符capLetters中的第一個字符進行比較即可將該字符串添加到列表中。您應該檢查所有字符,然後將該字添加到循環外的列表中。使用boolean變量。

1

因爲你的循環是不正確的,請嘗試以下代碼:

while((str=br.readLine())!=null) 
     { 


      boolean foundCap = false; 
      //Remove the words with first letter capital 
      for(int i=0;i<capLetters.length;i++) 
      { 
       if(str.charAt(0)==capLetters[i]) 
       { 
        foundCap = true; 
       } 

      }    
      if(!foundCap) { 
       wordList.add(str); 
      } 

      number++; 

     } 

畢竟,你不需要寫一個循環來檢查字符是否大寫。只需使用工具方法: Character.isUpperCase(char c)

0
boolean b=false; 
for(int i=0;i<capLetters.length;i++) 
       { 
        if(str.charAt(0)==capLetters[i]) 
        { 
         b=true; 
        } 

       } 
      if(!b){ 
       wordlist.add(str); 
      }   

順便說一句,你是在浪費時間和空間,通過字符數組循環,您可以比較字母的字符的ASCII碼。 你可以做

if(str.charAt(0)>=65 && str.charAt(0)<=90) 
//donothing 
else 
wordlist.add(str); 
0

雖然,你已經進行補正,被別人建議後寫,當然,邏輯,也有其他方式,似乎更清潔,我(和希望這將是同樣的話還有)

//create a list from the caps array, to be used for comparision 
List<char[]> caps = Arrays.asList(capLetters); 

現在你而應該是這樣的:

while((str=br.readLine())!=null) 
{ 
    //add word only if first letter is not capital 
    if(!caps.contains(str.charAt(0)) 
     wordList.add(str); 
    } 
    number++; 
} 
相關問題