从另一个方法传递数组作为参数

时间:2015-11-12 19:48:19

标签: java file-io spell-checking

我正在尝试读取文本文件,然后将这些单词与字典文本文件进行比较,并打印出dictionary.txt文件中找不到的任何单词。我的问题是我无法弄清楚如何将字典数组从dictionaryRead()方法传递给binarySearch()方法作为参数。

    public class spellChecker {
    public static void main(String[] args) throws Exception
    {
        spellChecker spellcheck = new spellChecker();
        spellcheck.textFileRead(spellcheck.dictionaryRead());
    }

    private String[] dictionaryArray; 
    public String[] dictionaryRead() throws Exception
    {
        // Find and read the file into array
        String token = "";

        // Use scanner for input file
        Scanner dictionaryScan = new Scanner(new File("dictionary.txt")).useDelimiter(",\\s*"); 

        List<String> dictionary = new ArrayList<String>();

        //Check for next line in text file
        while (dictionaryScan.hasNext()) 
        {
          token = dictionaryScan.next();
          dictionary.add(token);
        }
        dictionaryScan.close();

        dictionaryArray = dictionary.toArray(new String[0]);

        return dictionaryArray;
    }

    public void textFileRead(String [] dictionaryArray) throws Exception
    {
        //Get input file path from user
        System.out.println("Please enter a file path: ");
        Scanner scan = new Scanner(System.in);
        String inputFile = scan.nextLine();
        scan.close();

        // Find and read the file
        String word = "";
        // Use scanner for input file
        Scanner inputFileScan = new Scanner(new File(inputFile + ".txt")).useDelimiter(",\\s*");

        //Check for next line in text file
        while (inputFileScan.hasNext()) 
        {
          word = inputFileScan.nextLine();
          binarySearch(word);
        }

        inputFileScan.close();
    }

    public void binarySearch(String word)
    {
        int first = 0;
        int last  = dictionaryArray.length;

        while (first < last) {
          int mid = first + ((last - first) / 2);
          if (word.compareTo(dictionaryArray[mid]) < 0) {
              last = mid;
          } else if (word.compareTo(dictionaryArray[mid]) > 0) {
              first = mid + 1;
          } else {
              if(word != dictionaryArray[mid]){
                System.out.println(word);
              }
          }
        }  
    }
} 

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,因为你要创建一个类的实例,你可以在你的类中使dictionaryArray成为一个字段:

private String[] dictionaryArray; 

public void dictionaryRead() throws FileNotFoundException {
    ...
    // Note how String[] is removed on the line below:
    dictionaryArray = dictionary.toArray(new String[0]);
}

现在dictionaryArray对您班级的所有实例方法都可见,因此您只需在binarySearch方法中按名称引用它:

public void binarySearch(String word) {
    // Use dictionaryArray instead of arr
    ...
}

答案 1 :(得分:1)

稍微改变你的结构

public class spellChecker {
    public static void main(String[] args) throws IOException 
    {
        spellChecker spellcheck = new spellChecker();

        spellcheck.textFileRead(spellcheck.dictionaryRead());
    }

    public String [] dictionaryRead() throws FileNotFoundException
    {
        // Find and read the file into array
        String token = "";

        // Use scanner for input file
        Scanner dictionaryScan = new Scanner(new File("dictionary.txt")).useDelimiter(",\\s*");

        List<String> dictionary = new ArrayList<String>();

        //Check for next line in text file
        while (dictionaryScan.hasNext()) 
        {
          token = dictionaryScan.next();
          dictionary.add(token);
        }
        dictionaryScan.close();

        String[] dictionaryArray = dictionary.toArray(new String[0]);

        return dictionaryArray;
    }

    public void textFileRead(String [] dictionaryArray) throws IOException
    {
        //Get input file path from user
        System.out.println("Please enter a file path: ");
        Scanner scan = new Scanner(System.in);
        String inputFile = scan.nextLine();
        scan.close();

        // Find and read the file
        String word = "";
        // Use scanner for input file
        Scanner inputFileScan = new Scanner(new File(inputFile + ".txt")).useDelimiter(",\\s*");

        //Check for next line in text file
        while (inputFileScan.hasNext()) 
        {
          word = inputFileScan.next();
          binarySearch(dictionaryArray[], word);
        }

        inputFileScan.close();
    }

    public void binarySearch(String[] arr, String word)
    {
        int first = 0;
        int last  = arr.length;

        while (first < last) {
          int mid = first + ((last - first) / 2);
          if (word.compareTo(arr[mid]) < 0) {
              last = mid;
          } else if (word.compareTo(arr[mid]) > 0) {
              first = mid + 1;
          } else {
              if(word != arr[mid]){
                System.out.println(arr[mid]);
              }
          }
        }  
    }
}
相关问题