简体   繁体   中英

remove int, char from string

Removing particular characters and all integers from strings read in from a file.

The goal was to scrub all non-sentence ending characters and numbers from a string that is being read in through a scanner. The reason for removing these chars and ints was to generate an accurate word count, sentence count, and syllable count from the words that are read in.

The original posted code is very rough, has been fixed, and will be re-posted below from myself.

Thank you for all of your time and help with this!

public class Word {
    private int wordCount, sentenceCount, syllableCount;
    private int nums [] = {1,2,3,4,5,6,7,8,9,0};
    private char vowels [] = {'a', 'e', 'i', 'o', 'u', 'y'};;
    private char punctuation [] = {'!', '?','.', ';', ':','-','"','(', ')'};;

    public Word()
    {
        wordCount = 0;
        sentenceCount = 0;
        syllableCount =0;
    }

    public Word(String next)
    {
        if(next.length() > 1)
        {
            //
            // Remove punctuation that does not create sentences
            //
            for(int i = 5; i < punctuation.length; i++)
                for(int j = 0; j < next.length(); j++)
                    if(next.charAt(j) == punctuation[i])
                            next = next.replace(j, "");
            //
            // Counting Sentences
            //
            for(int i=0; i < 5; i++)
                if(punctuation[i] == next.charAt(next.length()-1))
                    sentenceCount++;
            //
            // Remove numbers for accurate word counting
            //
            for(int i = 0; i < nums.length; i++)
                for(int j = 0; j < next.length(); j++)
                    if(next.charAt(j) == nums[i])
                        next = next.replace(j, "");
            //
            // Counts all syllables
            //
            for(int i = 0; i < vowels.length; i++)
                for(int j = 0; j < next.length()-1; j++)
                    if(vowels[i] == next.charAt(j))
                        syllableCount++;
            System.out.println(next);
        }
    }

you can't write

next.return(j,"");// you are replacing the j value(int) with ""

since .replace() is something that replaces only String with another string.. even char cannot be replaced.. so you need to type cast..

so i tried and edited your code.. let me know of you have some problem...

public class Word {
private int wordCount, sentenceCount, syllableCount;
private int nums [] = {1,2,3,4,5,6,7,8,9,0};
private char vowels [] = {'a', 'e', 'i', 'o', 'u', 'y'};
private char punctuation [] = {'!', '?','.', ';', ':','-','(', ')'};

public Word()
{
    wordCount = 0;
    sentenceCount = 0;
    syllableCount =0;
}

public Word(String next)
{
    System.out.println("The Given String is"+next);
    if(next.length() > 1)
    {
        int n=0;
        int a=0;
            if(next.charAt(0)=='"'){n=1;}
        if(next.charAt(next.length()-1)=='"'){a=1;}

        for(int y=0+n; y<next.length()-a;y++){
             if(next.charAt(y)=='"'){
                next=next.substring(0,y)+next.substring(y+1,next.length());
                                    }}
        for(int i=0;i<8;i++){
              char k=punctuation[i];
              String l= Character.toString(k);
              int j=next.indexOf(k);
              if (next.contains(l)) {next=next.replace(l,"");}}

        for(int i=0;i<10;i++){
              int k=nums[i];
              String q= Integer.toString(k);
              if (next.contains(q)){
                  next=next.replace(q, "");}}

System.out.println("The String After Editing is: "+next);  

}}}

public class Word {
    private int wordCount, sentenceCount, syllableCount;
    private char vowels [] = {'a','e','i','o','u','y','A','E','I','O','U','Y'};
    private char punctuation [] = {'!','?','.',';',':','-','"','(',')',','};

    public Word()
    {
        wordCount = 0;
        sentenceCount = 0;
        syllableCount = 0;
    }

    public Word(String next)
    {
        // Remove numbers
        next = next.replaceAll("[0-9]", "");
        // Skip over blank tokens
        if(next.contains(" ") || next.length() < 1)
                return;

        // String builder method used for removing all non-sentence ending
        // grammar marks.
        StringBuilder newNext = new StringBuilder(next);
        String tempNext;
        if(newNext.length() > 0)
        {
            for(int i = 5; i < punctuation.length; i++)
                for(int j = 0; j < newNext.length(); j++)
                    if(punctuation[i] == newNext.charAt(j))
                    {
                         newNext = newNext.deleteCharAt(j);
                    }
            tempNext = newNext.toString();
            next = tempNext;

            if(next.length() < 1)
                return;
            else
                wordCount++;
        }

        // Count sentences
        for(int i = 0; i < 5; i++)
            if(next.charAt(next.length()-1) == punctuation[i])
                sentenceCount++;

        // Counts all syllables
        if(next.length() > 2)
        {
            for(int i = 0; i < vowels.length; i++)
                for(int j = 0; j < next.length()-1; j++)
                    if(vowels[i] == next.charAt(j))
                        syllableCount++;
        }
        //System.out.println(next);
    }

If word is your method.. then its an invalid declaration... For Any method you create you need to have a return type.. ie,

If method returns value(suppose int) then it need to be

public int word(String S){}

If the method doesn't return any value then it need to be

public void word(String S){}

If you want to construct a constructor.. the constructor name and class name need to be same..

public World(String S){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM