2011-03-16 67 views
1

當我嘗試將字符串轉換爲int數組時,我得到一個空指針異常。我的字符串數組沒有被使用。我的數組得到一個空指針異常?

如何將int數組更改爲字符串數組,或者如何解決我的問題?

這是我的代碼。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication3; 

/** 
* 
* @author Ivan Beazer 
*/ 
import java.io.*; 

/** 
    This program demonstrates the search method in 
    the IntBinarySearcher class. 
*/ 

public class BinarySearchTest 
{ 
    private static String aString; 
    // Convert string array to string 
    public static String arrayToString2(String[] words, String aString) 
    { 
     StringBuilder result = new StringBuilder(); 
     if (words.length > 0) 
     { 
      result.append(words[0]); 
      for (int i=1; i<words.length; i++) 
      { 
       result.append(aString); 
       result.append(words[i]); 
      } 
     } 
     return result.toString(); 
    }  

    public static void main(String [] args) throws IOException 
    { 
     int result, searchValue; 
     String input; 

     // A String array of words to search. 
     // This is the error. netbeans says it's not being used. 
     String[] words = {"Jake", "Jerry", "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"}; 



     // convert string to int array 

     // this is my problem 
     int[] numbers = new int[aString.length()]; 
     for(int i=0; i<aString.length(); i++) 
     numbers[i] = Character.getNumericValue(aString.charAt(i)); 

     // this is me trying to fix the problem 
     if(numbers != null) 
     { 
      //do something to num 

     } 

     // Create the console input objects. 
     InputStreamReader reader = 
       new InputStreamReader(System.in); 
     BufferedReader keyboard = 
       new BufferedReader(reader); 

     // First we must sort the array in ascending order. 
     IntQuickSorter.quickSort(numbers); 

     do 
     { 
     // Get a value to search for. 
     System.out.print("Enter a value to search for: "); 
     input = keyboard.readLine(); 
     searchValue = Integer.parseInt(input); 

     // Search for the value 
     result = IntBinarySearcher.search(numbers, searchValue); 

     // Display the results. 
     if (result == -1) 
      System.out.println(searchValue + " was not found."); 
     else 
     { 
      System.out.println(searchValue + " was found at " + 
           "element " + result); 
     } 

     // Does the user want to search again? 
     System.out.print("Do you want to search again? (Y or N): "); 
     input = keyboard.readLine(); 
     } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y'); 
    } 
} 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication3; 

/** 
* 
* @author Devon B 
*/ 
/** 
    The IntBinarySearcher class provides a public static 
    method for performing a binary search on an int array. 
*/ 

public class IntBinarySearcher 
{ 



    /** 
     The search method performs a binary search on an int 
     array. The array is searched for the number passed to 
     value. If the number is found, its array subscript is 
     returned. Otherwise, -1 is returned indicating the 
     value was not found in the array. 
     @param array The array to search. 
     @param value The value to search for. 
    */ 

    public static int search(int[] array, int value) 
    { 
     int first;  // First array element 
     int last;  // Last array element 
     int middle;  // Mid point of search 
     int position; // Position of search value 
     boolean found; // Flag 

     // Set the inital values. 
     first = 0; 
     last = array.length - 1; 
     position = -1; 
     found = false; 

     // Search for the value. 
     while (!found && first <= last) 
     { 
     // Calculate mid point 
     middle = (first + last)/2; 

     // If value is found at midpoint... 
     if (array[middle] == value) 
     { 
      found = true; 
      position = middle; 
     } 
     // else if value is in lower half... 
     else if (array[middle] > value) 
      last = middle - 1; 
     // else if value is in upper half.... 
     else 
      first = middle + 1; 
     } 

     // Return the position of the item, or -1 
     // if it was not found. 
     return position; 


    } 
} 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication3; 

/** 
* 
* @author Devon B 
*/ 
/** 
    The IntQuickSorter class provides a public static 
    method for performing a QuickSort on an int array. 
*/ 

public class IntQuickSorter 
{ 
    /** 
     The quickSort method calls the doQuickSort method 
     to sort an int array. 
     @param array The array to sort. 
    */ 

    public static void quickSort(int array[]) 
    { 
     doQuickSort(array, 0, array.length - 1); 
    } 

    /** 
     The doQuickSort method uses the QuickSort algorithm 
     to sort an int array. 
     @param array The array to sort. 
     @param start The starting subscript of the list to sort 
     @param end The ending subscript of the list to sort 
    */ 

    private static void doQuickSort(int array[], int start, int end) 
    { 
     int pivotPoint; 

     if (start < end) 
     { 
     // Get the pivot point. 
     pivotPoint = partition(array, start, end); 

     // Sort the first sub list. 
     doQuickSort(array, start, pivotPoint - 1); 

     // Sort the second sub list. 
     doQuickSort(array, pivotPoint + 1, end); 
     } 
    } 

    /** 
     The partiton method selects a pivot value in an array 
     and arranges the array into two sub lists. All the 
     values less than the pivot will be stored in the left 
     sub list and all the values greater than or equal to 
     the pivot will be stored in the right sub list. 
     @param array The array to partition. 
     @param start The starting subscript of the area to partition. 
     @param end The ending subscript of the area to partition. 
     @return The subscript of the pivot value. 
    */ 

    private static int partition(int array[], int start, int end) 
    { 
     int pivotValue; // To hold the pivot value 
     int endOfLeftList; // Last element in the left sub list. 
     int mid;   // To hold the mid-point subscript 

     // Find the subscript of the middle element. 
     // This will be our pivot value. 
     mid = (start + end)/2; 

     // Swap the middle element with the first element. 
     // This moves the pivot value to the start of 
     // the list. 
     swap(array, start, mid); 

     // Save the pivot value for comparisons. 
     pivotValue = array[start]; 

     // For now, the end of the left sub list is 
     // the first element. 
     endOfLeftList = start; 

     // Scan the entire list and move any values that 
     // are less than the pivot value to the left 
     // sub list. 
     for (int scan = start + 1; scan <= end; scan++) 
     { 
     if (array[scan] < pivotValue) 
     { 
      endOfLeftList++; 
      swap(array, endOfLeftList, scan); 
     } 
     } 

     // Move the pivot value to end of the 
     // left sub list. 
     swap(array, start, endOfLeftList); 

     // Return the subscript of the pivot value. 
     return endOfLeftList; 
    } 

    /** 
     The swap method swaps the contents of two elements 
     in an int array. 
     @param The array containing the two elements. 
     @param a The subscript of the first element. 
     @param b The subscript of the second element. 
    */ 

    private static void swap(int[] array, int a, int b) 
    { 
     int temp; 

     temp = array[a]; 
     array[a] = array[b]; 
     array[b] = temp; 
    } 
} 
+0

變量* aString *初始化在哪裏? – Raghuram 2011-03-16 05:26:36

回答

4

你會得到一個NullPointerException,因爲變量「aString」在你說「這是問題」的那一行是空的。你從來沒有設置過任何東西。我不能說我真的明白你要在這裏做什麼,所以我不能再幫助你。

+0

我不明白我用字符串生成器來設置字串數組的aString。 – lonesarah 2011-03-16 05:32:23

+0

程序中沒有任何地方提到「aString = X」,表示任何X值。每次提到aString時 - 包括代碼中的StringBuilder部分 - 你都*讀* aString的值,而不是設置它對任何事物。 – 2011-03-16 05:35:42

+0

好吧,現在我正在看我在做什麼。 – lonesarah 2011-03-16 05:38:32

0

你不需要if(數字!= null)作爲「數字」永遠不能爲null(除非OOME發生,這裏不是這種情況),因爲你正在實例化它之前的幾行。但問題可能是因爲你的aString可能爲空。

0

aString在哪裏被初始化?我會說這就是爲什麼你得到一個空指針execption。