Java如何将字符串数组中的小写字符串值转换为大写字符串值
我有这个问题,我希望当用户输入小写单词时。我希望我的程序在数组中使用小写字符串,在数组中使用大写字符串Java如何将字符串数组中的小写字符串值转换为大写字符串值,java,arrays,eclipse,string,Java,Arrays,Eclipse,String,我有这个问题,我希望当用户输入小写单词时。我希望我的程序在数组中使用小写字符串,在数组中使用大写字符串 /** 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 Object
/**
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;
}
}
/**
ObjectSelectionSorter类提供了一个公共静态
用于对多个对象执行选择排序的方法
实现可比较接口的对象。
*/
公共类ObjectSelectionSorter
{
/**
selectionSort方法对数据执行选择排序
实现可比较接口的对象数。
@param number要排序的数字。
*/
公共静态无效选择排序(可比[]个数字)
{
int startScan;//扫描的开始位置
int index;//保存下标值
int minIndex;//扫描中具有最小值的元素
Compariable minValue;//在扫描中找到的最小值
//外部循环对中的每个元素迭代一次
//数字。startScan变量标记以下位置:
//扫描应该开始了。
对于(startScan=0;startScan<(数字.长度-1);startScan++)
{
//假设可扫描区域中的第一个图元
//是最小的值。
minIndex=startScan;
minValue=数字[startScan];
//从中的第二个元素开始扫描数字
//可扫描区域。我们正在寻找最小的
//可扫描区域中的值。
对于(索引=startScan+1;索引 而(!found&&first有string.toUpperCase()
和Character.toUpperCase(char)
。前者返回的字符串是给定字符串的大写版本,后者返回的是char
的大写版本。(我给出后者是因为你提到了一个数组,你的意思可能是一个字符数组)试试这个:
String[] numbers = {"Jerry"};
numbers[0] = numbers[0].toUpperCase();
请就代码的特定部分提出特定问题。