2011-03-16 3 views
-1

문자열 이진 검색 프로그램을 만들려고합니다. 문제는 문자열 배열을 정수 배열로 변환하는 간단한 방법을 기억하지 못합니다.내 프로그램에서 식별자가 필요하다고합니다.

나는 그들을 길고 복잡한 방법으로 작성했습니다. 그러나 Netsbeans는 문자열 배열 식별자가 필요하다고 말하고 있습니다. (.) : 문자열 "Jerry"

/* 
* 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 identifier is expected. 
     String[] words = {"Jake", "Jerry". "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"}; 

     // convert string to int array 

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

     // 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

사람이 알고 있나요 문자열을 int 문자열 배열을 변환하는 쉬운 방법 . – lonesarah

+0

해결되었습니다. 다음 번에 코드를 선택하고 Ctrl + K를 눌러 포맷하십시오. – casablanca

+0

감사합니다 casablanca – lonesarah

답변

1

String[] words = {"Jake", "Jerry". "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"}; 

당신은 점을 대신 쉼표 (,)

+0

와우, 고마워. Ken Wayne VanderLinde에게 감사 드려요. 시간이 필요했습니다. – lonesarah

+0

문자열 배열을 정수 배열로 변환하는 더 쉬운 방법을 알고 계십니까? – lonesarah

+0

찾고있는 전환 유형을 정확하게 확장 할 수 있습니까? –

관련 문제