Не знаю, почему это не сработает.
Я написал этот код. Я должен инициировать массив так, как он есть. Профессор хочет, чтобы мы это сделали. Но он не будет проходить через код и заканчиваться. Не знаю почему. Любая помощь, пожалуйста.
Что я уже пробовал:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import static java.lang.System.out; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; /** * * * @author tsadams */ public class Program2 { private int n = 235886; private int mid = n/2; int correctWords = 0; int incorrectWords = 0; public Program2() { mid = 0; } /** * @param args the command line arguments */ /** * * @author tsadams */ /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException, IOException { File inf = new File("dictionary.txt"); String[] sArray = new String[238886]; Program2 a = new Program2(); a.readDictionary(sArray); Arrays.sort(sArray); int correctRec = 0; int incorrectRec = 0; // String data[] = new String[10]; // data = s.split("\\n"); //System.out.print(contains(data, "A")); FileInputStream infO = new FileInputStream(new File("oliver.txt")); char let; String str = ""; //word to be processed int n = 0; while ((n = infO.read()) != -1) { let = (char) n; if (Character.isLetter(let)) { str += Character.toLowerCase(let); } if ((Character.isWhitespace(let) || let == '-') && !str.isEmpty()) { if (a.bSearch(sArray, str, 0, 235886) >= 0) { correctRec++; } else { incorrectRec++; } str = ""; } } infO.close(); a.print(correctRec, incorrectRec); /** * * */ /** * * @param correctRec * @param incorrectRec * @param incorrectWords * @param correctWords */ } public void print(int correctRec, int incorrectRec) { System.out.println("Out of total words" + (incorrectWords + correctWords)); System.out.println("Correc" + correctWords); System.out.println("incorrect" + incorrectWords); System.out.println("Total number of recursive steps is" + (correctRec + incorrectRec)); System.out.println("The average number of comparisons for a word found =" + correctRec/correctWords); System.out.println("The average number of comparisons for a word not found = " + incorrectRec/incorrectWords); } public void readDictionary(String[] sArray) { try { int i = 0; File f = new File("dictionary.txt"); Scanner inf = new Scanner(f); while (inf.hasNext()) { sArray[i] = inf.nextLine(); i++; } } catch (FileNotFoundException ex) { System.out.println("The dictionary file was not found"); } } public int bSearch(String[] sArray, String key, int lowIndex, int highIndex) { int rec = 1; if (lowIndex > highIndex) { System.out.println(key + "is possibly misspelled"); incorrectWords++; return rec * -1; } mid = (lowIndex + highIndex) / 2; if (sArray[mid].equals(key)) { correctWords++; return rec; } else if (sArray[mid].compareToIgnoreCase(key) > 0) { rec++; return bSearch(sArray, key, lowIndex, mid - 1); } else { rec++; return bSearch(sArray, key, mid + 1, highIndex); } } }