简体   繁体   中英

how to access a method from private static class

im trying to put all stopwords on a hashset, i dont want to add it one by one so im trying to put in a txt file and have my scanner scan it. the problem is i think my code does not reach my scanner here is my code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class StopWords  {

    public static final Set<String> stopWords = new HashSet<String>();   

    private static class scan {

        public scan()throws IOException {
            Scanner s = null;

            try{
                s = new Scanner(new BufferedReader(new FileReader("stopwords.txt")));

                while (s.hasNext()) {
                    //System.out.println(s.next());
                    stopWords.add(s.next());
                }
            }finally{
                if (s != null) {
                    s.close();
                }
            }        
        }
    }
}

im running my main on other class and im just calling this class. thanks in advance

Make a wrapper for it in enclosing class. Something like:

public void doScan() {
    try {
         scan.scan();
    catch (IOException e) {};
}

in StopWords class.

This way you could call doScan() on instance of StopWords. You could also make it static.

And I agree that you should follow naming convections of Java language ( wikipedia.org ).

Just want to add a couple tricks you might consider:

  • First - you could store your stopwords in a properties file, then use java.util.Properties.load to pull the data in.
  • Second - you can put your stopwords file on your classpath, and bundle up the stopwords file with the rest of your code in a jar for delivery.

You wind up with something like this:

final Properties stopProps = new java.util.Properties();
stopProps.load( new InputStreamReader( this.class.getClassLoader().getResourceAsStream( "mycode/stopWords.properties", "UTF-8" ) );

...

Good luck!

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