-1

私はこのウェブサイトを初めて利用していて、Javaには慣れていません。私はここで、このプログラムを持っている内容は以下のとおりです。入力ミスマッチ例外エラーメッセージ?

public class KNW_NameSearch 
{ 
    /** 
    * The quickSort method, will sort the array of strings 
    * @param femaleNames, The array of female names. 
    * */ 
    public void quickSort (int femaleNames[]) 
    { 
    doQuickSort(femaleNames, 0, femaleNames.length - 1); 
    } 

    /** 
    * The doQuickSort method, will execute the quick sort. 
    * @param femaleNames, The array of female names. 
    * @param start, The starting subscript. 
    * @param end, The ending subscript. 
    * */ 
    private void doQuickSort(int femaleNames[], 
          int start, int end) 
    { 
    //Declare pivot point 
    int pivotPt; 

    if(start > end) 
    { 
     //Get the pivot point 
     pivotPt = partition(femaleNames, start, end); 

     //Sort the first sublist 
     doQuickSort(femaleNames, start, pivotPt - 1); 

     //Sort the second sublist 
     doQuickSort(femaleNames, pivotPt + 1, end); 
    } 
    } 

    /** 
    * The parition method, determines the pivot value and rearranges 
    * the array. 
    * @param femaleNames, The array of female names. 
    * @param start, The starting subscript. 
    * @param end, The ending subscript. 
    * */ 
    private int partition(int femaleNames[], 
         int start, int end) 
    { 
    int pivotValue; 
    int endOfLeftList; 
    int mid; 

    //Find the middle element 
    mid = (start + end)/2; 

    //Call the swap method 
    swap(femaleNames, start, mid); 

    //Save pivotValue 
    pivotValue = femaleNames[start]; 

    //Save endOfLeftList 
    endOfLeftList = start; 

    //A for-loop to sort the names in a given array 
    for(int x = start + 1; x <= end; x++) 
    { 
     if(femaleNames[x] < pivotValue) 
     { 
     endOfLeftList++; 

     //Call the swap method 
     swap(femaleNames, endOfLeftList, x); 
     } 
    } 

    swap(femaleNames, start, endOfLeftList); 

    return endOfLeftList; 
    } 

    /** 
    * The swap method, exchanging values in the provided subscripts. 
    * @param femaleNames, The array of femaleNames. 
    * @param a, A subscript to be swapped. 
    * @param b, A subscript to be swapepd. 
    * */ 
    private void swap(int femaleNames[], 
        int a, int b) 
    { 
    //Perform swap 
    int temp; 
    temp = femaleNames[a]; 
    femaleNames[a] = femaleNames[b]; 
    femaleNames[b] = temp; 
    } 

    /** 
    * The binarySearch method, will perform a binary search 
    * on a sorted array. 
    * @param fNames, The array of names. 
    * @param names, A string of names 
    * */ 
    public int binarySearch(int fNames[], int names) 
    { 
    //Return the doBinarySearch method 
    return doBinarySearch(fNames, 0, fNames.length - 1, names); 
    } 

    /** 
    * The doBinarySearch method, will execute a binary search 
    * @param fNames, The array of names 
    * @param first, The first subscript. 
    * @param last, The last subscript. 
    * @param name, A string of names 
    * */ 
    private int doBinarySearch(int fNames[], int names, 
          int first, int last) 
    { 
    int middle; 

    //Return -1 if the name isn't in the array 
    if(first > last) 
    { 
     return -1; 
    } 

    //Find the middle position 
    middle = (first + last)/2; 

    //Find the position of the given name 
    if(fNames[middle] == names) 
    { 
     return doBinarySearch(fNames, middle + 1, 
          last, first); 
    } 
    else if(fNames[middle] < names) 
    { 
     return doBinarySearch(fNames, first, 
          middle - 1, names); 
    } 
    else 
    { 
     return middle; 
    } 
    } 
} 

デモ:

public class KNW_NameSearchDemo 
{ 
    public static void main(String args[]) throws IOException 
    { 
    //Declare the variables 
    int searchName; 
    int firstNames[] = new int[65]; 
    String choice; 
    int result; 

    //Create the scanenr object 
    Scanner scanner = new Scanner(System.in); 

    //Create an object for KNW_NameSearch 
    KNW_NameSearch kns = new KNW_NameSearch(); 

    //Open the file 
    File file = new File("names.txt"); 
    Scanner name = new Scanner(file); 

    //Read the lines in the file until there is no more 
    for(int x = 0; x < 65; x++) 
    { 
     //Read the next line 
     firstNames[x] = name.nextInt(); 
    } 



    //Print the names before sorting array 
    System.out.println("Before sorting: \n"); 
    for(int x = 0; x < 65; x++) 
    { 
     System.out.println(firstNames[x]); 
    } 

    //Call to quickSort 
    kns.quickSort(firstNames); 

    //Search about a name 
    do 
    { 
     //Print the names after sorting array 
     System.out.println("\nPlease enter a name to search: "); 
     searchName = scanner.nextInt(); 

     //Call to binarySearch method 
     result = kns.binarySearch(firstNames, searchName); 

     //Print the result 
     if(result == -1) 
     { 
     System.out.println("\n" + searchName + "wasn't found!"); 
     } 
     else 
     { 
     System.out.println("\n" + searchName + "was found at position " 
          + (result + 1)); 
     } 

     //Does the user want to use again 
     System.out.print("\n-------\nDo you want to use again?: "); 
     choice = scanner.nextLine(); 
    } 
    //if the user said yes 
    while(choice.charAt(0) == 'y'); 
    } 
} 

私はInputMismatchErrorを取得していますし、私は本当にそれをすべて理解することはできませんが。彼らがどこにいるのか私には分かりません。ここではエラーメッセージは次のとおりです。

エラー:

java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at KNW_NameSearchDemo.main(KNW_NameSearchDemo.java:33) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267) 

は、誰もがこのエラーメッセージが表示されるように引き起こしているものを知っていますか?私はテキストファイルのリストでバイナリ検索をしようとしています。前はクイックソートを実行します。ありがとう、私は新しいので、簡単に行く!

答えて

1

ライン

at KNW_NameSearchDemo.main(KNW_NameSearchDemo.java:33) 

は、あなたのコード内の例外はあなたが完全なソースを提供しなかったライン33で呼び出された関数内のどこかに投げられると言っているが、私はラインがあると仮定します。

firstNames[x] = name.nextInt(); 

ランタイムはInputMismatchExceptionをスローします。つまり、nextInt関数はファイル内に整数のように見えないものを見ます。変数firstNamesの名前が与えられていれば、そこに文字列があると想定します。したがって、あなたの間違いはfirstNamesintの配列であると宣言したことです。代わりにStringオブジェクトの配列(またはより良いArrayList)でなければなりません。

また、ソースコードに正確な行数(つまり、65)を書きます。これは非常に悪いスタイルです。 SOの場合でも、例えばthisのようなソリューションのいくつかを見つけることができます。またはthisのようなインターネット上の他の場所。私はちょうど彼らが行数のための固定数を使用しないことを示すためにいくつかの任意のリンクを取った。

関連する問題