2011-03-19 2 views
1

나는이 문제가 있고 나는 사용자가 소문자 단어를 입력 할 때 그것을 원한다. 배열에서 배열과 대문자 문자열에서 소문자 문자열을 사용하도록 프로그램을 만들고 싶습니다.자바 어떻게 문자열 배열에서 대문자로 소문자 문자열 값을 변환합니까

/** 
    The ObjectSelectionSorter class provides a public static 
    method for performing a selection sort on an numbers of 
    objects that implement the Comparable interface. 
*/ 

public class ObjectSelectionSorter 
{ 

    /** 
     The selectionSort method performs a selection sort on an 
     numbers of objects that implement the Comparable interface. 
     @param numbers The numbers to sort. 
    */ 

    public static void selectionSort(Comparable[] numbers) 
    { 
     int startScan;  // Starting position of the scan 
     int index;   // To hold a subscript value 
     int minIndex;  // Element with smallest value in the scan 
     Comparable minValue; // The smallest value found in the scan 

     // The outer loop iterates once for each element in the 
     // numbers. The startScan variable marks the position where 
     // the scan should begin. 
     for (startScan = 0; startScan < (numbers.length-1); startScan++) 
     { 
     // Assume the first element in the scannable area 
     // is the smallest value. 
     minIndex = startScan; 
     minValue = numbers[startScan]; 

     // Scan the numbers, starting at the 2nd element in 
     // the scannable area. We are looking for the smallest 
     // value in the scannable area. 
     for(index = startScan + 1; index < numbers.length; index++) 
     { 
      if (numbers[index].compareTo(minValue) < 0) 
      { 
       minValue = numbers[index]; 
       minIndex = index; 
      } 
     } 

     // Swap the element with the smallest value 
     // with the first element in the scannable area. 
     numbers[minIndex] = numbers[startScan]; 
     numbers[startScan] = minValue; 
     } 
    } 
} 




import java.io.*; 

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

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

     // An array of numbers to search. 
     String[] numbers = {"Jake", "Jerry", "Bill", "Lousie", "Goku", "Ivan", "John", "Sarah", "Kim"}; 


     // 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. 
     ObjectSelectionSorter.selectionSort(numbers); 

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


     // Search for the value 
      result = ObjectBinarySearcher.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'); 
    } 
} 



/** 
    The StringBinarySearcher class provides a public static 
    method for performing a binary search on an String array. 
*/ 



public class ObjectBinarySearcher{ 

/** 
     The search method performs a binary search on an String 
     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 numbers The array to search. 
     @param value The value to search for. 
    */ 



    public static int search(String[] numbers, String 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 initial values. 
     first = 0; 
     last = numbers.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 (numbers[middle].equals(value)) 
     { 
      found = true; 
      position = middle; 
     } 

     // else if value is in lower half... 
     // need tell is value is less then the integer?, with out using equality regulators 
     else if (value.compareTo(numbers[middle]) < 0) 
      { 

      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; 
    } 
} 
+0

이 코드의 특정 부분에 대해 구체적인 질문을하십시오 . – Gabe

답변

2

이 시도 :

String[] numbers = {"Jerry"}; 
numbers[0] = numbers[0].toUpperCase(); 
1

string.toUpperCase()Character.toUpperCase(char)있다. 전자는 주어진 문자열의 대문자 버전 인 문자열을 반환하고 후자는 char의 대문자 버전을 반환합니다. (당신은 배열을 언급 때문에 나는 후자를주는거야, 그리고 당신이 말은 문자의 배열 수 있습니다)

관련 문제