themrhornet
themrhornet

Reputation: 92

removing all character from string except a-z in array

i am trying to read words from the text file and store it in array.Problem from the code i tried as shown below is that it reads all characters such as "words," and "read." but i only want "words" and "read" in an array.

public String[] openFile() throws IOException
{
    int noOfWords=0;
    Scanner sc2 = new Scanner(new File(path));
    while(sc2.hasNext()) 
    {
         noOfWords++;
         sc2.next();
    }

    Scanner sc3 = new Scanner(new File(path));
    String bagOfWords[] = new String[noOfWords];
    for(int i = 0;i<noOfWords;i++)
    {
         bagOfWords[i] =sc3.next();
    }

    sc3.close();
    sc2.close();
    return bagOfWords;
}

Upvotes: 1

Views: 65

Answers (3)

Rogel Garcia
Rogel Garcia

Reputation: 1915

You probably want only letters. In this case, you can use Character.isLetter(char) method.

Snippet:

String token = "word1";
String newToken = "";
for (int i = 0; i < token.length(); i++) {
    char c = token.charAt(i);
    if(java.lang.Character.isLetter(c)){
        newToken += c;
    }
}
System.out.println(newToken);

Upvotes: 1

codeaholicguy
codeaholicguy

Reputation: 1691

Use this code:

for (int i = 0; i < noOfWords; i++) {
     bagOfWords[i] = sc3.next().replaceAll("[^A-Za-z0-9 ]", "");
}

Upvotes: 2

Mike
Mike

Reputation: 1231

Use regex replace :

replaceAll("([^a-zA-Z]+)","");

And apply that line to

bagOfWords[i] = sc3.next().replaceAll("([^a-zA-Z]+)","");

Upvotes: 3

Related Questions