Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除首字母大写的单词_Java_List_Arraylist_Io_Java Io - Fatal编程技术网

Java 删除首字母大写的单词

Java 删除首字母大写的单词,java,list,arraylist,io,java-io,Java,List,Arraylist,Io,Java Io,我试图忽略首字母大写的单词,并将其他单词添加到列表中。但不幸的是,它没有删除任何内容,所有单词都被添加到列表中。这里怎么了 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 =

我试图忽略首字母大写的单词,并将其他单词添加到
列表中。但不幸的是,它没有删除任何内容,所有单词都被添加到
列表中。这里怎么了

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();
    }


}
import java.io.*;
导入java.util.ArrayList;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入java.util.List;
公共班机{
私有字符[]大写字母={'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'};
私有StringBuffer strBuffer=新StringBuffer(“”);
private List wordList=new ArrayList();
公用干管()
{
文件f=新文件(“C:/xxx/COMMON.txt”);
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(f));
字符串str=“”;
整数=0;
而((str=br.readLine())!=null)
{
//删除首字母大写的单词

对于(int i=0;i仅当第一个字母是
caplets[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
}

for(int i=0;i仅当第一个字母是
大写字母[i]
,其中
i
从0开始时才进行检查。如果字符串的开头不是“A”,它将转到
其他
。您应该这样做:

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

for(int i=0;i不必维护
caplets
并将第一个字符与其中的每个字符进行比较,您可以直接使用方法


对于当前方法,只需将第一个字符与
caplets
中的第一个字符进行比较,即可将字符串添加到列表中。您应该检查所有字符,然后将单词添加到循环外的列表中。使用
布尔变量。

而不是维护
capletse> 并将第一个字符与其中的每个字符进行比较,您可以直接使用该方法

对于当前方法,您只需将第一个字符与
大写字母中的第一个字符进行比较,即可将字符串添加到列表中。您应该检查所有字符,然后将单词添加到循环外的列表中。使用
布尔变量。

您可以这样做

 while((str=br.readLine())!=null)
 {
     if(!Character.isUpperCase(str.charAt(0)))
     {
           wordList.add(str);
     }
     number++;
 }
你可以这样做

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

由于循环不正确,请尝试以下代码:

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++;

        }
while((str=br.readLine())!=null)
{
布尔值foundCap=false;
//删除首字母大写的单词

对于(int i=0;i,由于循环不正确,请尝试以下代码:

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++;

        }
while((str=br.readLine())!=null)
{
布尔值foundCap=false;
//删除首字母大写的单词
对于(int i=0;i
boolean b=false;
对于(int i=0;i=65&&str.charAt(0)
boolean b=false;

对于(int i=0;i=65&&str.charAt(0)尽管您所写的逻辑,当然在他人建议的更正之后,还有其他方法对我来说似乎更清晰(希望您也能这样做)


虽然,你写的逻辑,当然是在别人建议的纠正之后,还有其他方法对我来说更干净(希望你也一样)

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