PeakGen
PeakGen

Reputation: 23035

Removing words if the first letter is capital

I am trying to ignore the words with the first letter capital and add other words to the List. But unfortunately, it really didn't remove anything and all the words are added into the List. What is wrong here?

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


}

Upvotes: 0

Views: 796

Answers (6)

Maroun
Maroun

Reputation: 95978

You're checking only if the first letter is capLetters[i], where i begins from 0. If the word string begin with something else than "A", it'll go to the else. You should do something like this:

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

Of course don't forget to initialize capitalFound to false, and to set it to false again before getting the next word.

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15664

Although, the logic you have written, of course after making correction suggested by others, there is also other way around that seems much cleaner to me (and hope it would be same for you as well)

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

Now your while should be like this :

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

Upvotes: 0

Roudy Tarabay
Roudy Tarabay

Reputation: 449

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

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

Btw,you are wasting time and space in looping through that character array,you can compare the letter to the ASCII Code of the characters. You can get do

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

Upvotes: 0

Weibo Li
Weibo Li

Reputation: 3605

Because your loop is not correct, try the following code:

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

        }

After all, you don't need to write a loop to check if a char is capital or not. Just use the utility method: Character.isUpperCase(char c)

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26104

You can do like this

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

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213311

Instead of maintaining capLetters and comparing the first character with each of the characters in it, you can directly use Character.isUpperCase(char) method.

For the current approach, you are adding the string into the list, by just comparing the first character with the first character in capLetters. You should check for all the characters instead, and then add the word to the list outside the loop. Use a boolean variable.

Upvotes: 1

Related Questions