我有一个简单的程序,要求您通过键盘输入数字(每个数字用空格或逗号分隔),然后将它们从低到高排序并打印出来。
问题是这些数字没有打印出来。
以下是源代码:
public class StartHere {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Type random numbers: ");
String input = new String(scanner.nextLine());
scanner.close();
String[] numString = new String[input.length()];
int a = 0;
int i = 0;
for (; i < input.length() - 1;) {
if (Character.isDigit(input.charAt(a))) { // If the character at input[a] is a digit
numString[i] += Character.toString(input.charAt(a)); // it is added to numString[i]
if(!(a+1 > input.length())){
a++;
}
}
if (numString[i] != null && !Character.isDigit(input.charAt(a))) { // If numString[i] is already in use and the char at input[a] is not a digit
if(!(i+1 > input.length())){
i++;
}
if(!(a+1 > input.length())){
a++;
}
}
if (numString[i] == null && !Character.isDigit(input.charAt(a))){ // If numString[i] is not in used and the character at char[a] is not a digit.
if(!(a+1 > input.length())){
a++;
}
}
}
a = 0;
i = 0;
int[] numbers = new int[numString.length];
for(; i < numString.length - 1; i++){
numbers[i] = Integer.parseInt(numString[i]);
}
quicksort(numbers, 0, numbers.length - 1);
for(i = 0; i < numbers.length - 1; i++){
if(i != numbers.length){
System.out.print(numbers[i] + ", ");
}else{
System.out.println(numbers[i] + ".");
}
}
}
public static void quicksort(int numbers[], int left, int right) {
int pivot = numbers[left]; // takes the first element as pivot
int l = left; // l searches from left to right
int r = right; // r searches from right to left
int aux;
while(l<r){ // While searches doesn't cross
while((numbers[l] <= pivot) && (l < r)) l++; // searches for an element higher than the pivot
while(numbers[r] > pivot) r--; // Searches for an element smaller than the pivot
if (l<r) { // if searches haven't been crossed
aux = numbers[l]; // they are exchanged
numbers[l] = numbers[r];
numbers[r] = aux;
}
}
numbers[left] = numbers[r]; // The pivot is placed in a way that we have the
numbers[r] = pivot; // smaller digits at the left and the higher digits at the right
if(left < r-1)
quicksort(numbers, left, r-1); // left subarray is sorted
if(r+1 < right)
quicksort(numbers, r+1, left); // right subarray is sorted
}
}
已编辑:在第26行添加了a++;
表达式,阻止程序进入无限循环和新的if
阻止程序阻止#{1}} 34;冻结&#34;,但现在我收到了这个错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16
at java.lang.String.charAt(Unknown Source)
at StartHere.main(StartHere.java:21)
这是输入:
Type random numbers: 246, 421, 123, 2
答案 0 :(得分:4)
我正在通过提供有效的替代方案来回答:
input.split("\\D+")
它只有一行。
根据大众的需求,以下是它的工作原理:
String[]
返回\D
个数字,由非数字分隔 - \D+
表示“非数字”,Arrays.stream()
表示一个或更多非数字map(Integer::parseInt)
从数组String
将每个Integer
从拆分转为10 > 2
。这是必需的,因此下一步将按数字顺序排序,而不是 lexographical 顺序 - n.b. "10" < "2"
但sorted()
map(String::valueOf)
对整数流进行排序(数字顺序)Integers
将Strings
转回collect()
,为下一步做好准备Collectors.joining(", ", "", ".")
将流合并到单个对象。请参阅下一点了解...... 答案 1 :(得分:1)
当你的程序读取第一个&#34;,&#34; ,
它会增加&#34; i&#34;,但是&#34; a&#34;没有增加,
input.charAt(a)将是&#39;,&#39;和numString [i]为空
因此将跳过if条件并导致无限循环。
答案 2 :(得分:0)
//Your Input String Is Like Type random numbers: 12,654,123,3
//you first need to split this string with (,) because
//scanner object read the line as string object so you need to fist split this input string with (,). then you can perform sort operation in easy way.
String[] splitStr = input.split(",");
答案 3 :(得分:0)
以下是代码示例,该数字将打印出来。
我修改了您的代码modified here
。我希望它会对你有所帮助。
<强>编辑:强>
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Type random numbers: ");
String input = new String(scanner.nextLine());
scanner.close();
// modified here -> split whitespace, a literal comma
String[] numString = input.split("\\s*,\\s*");
int i = 0;
int[] numbers = new int[numString.length];
for (; i < numString.length; i++) {
// modified here -> make sure numString[] isn't null and empty string
if (numString[i] != null && !"".equals(numString[i])) {
numbers[i] = Integer.parseInt(numString[i]);
}
}
quicksort(numbers, 0, numbers.length - 1);
// modified here
for (i = 0; i < numbers.length; i++) {
if (i < (numbers.length - 1)) {
System.out.print(numbers[i] + ", ");
} else {
System.out.println(numbers[i] + ".");
}
}
}
答案 4 :(得分:0)
试试这段代码。它只是做这项工作。您可以从逗号或空格分隔输入。但不是他们俩。
Scanner scanner = new Scanner(System.in);
System.out.print("Type random numbers: ");
String input = new String(scanner.nextLine());
scanner.close();
String arr[];
boolean isComma = false;
//check for comma
if (input.indexOf(",") != -1) {
isComma = true;
}
//split based on comma or space
if (isComma) {
arr = input.split(",");
} else {
arr = input.split(" ");
}
int series[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
series[i] = Integer.parseInt(arr[i].trim());
}
Arrays.sort(series);
for (int i = 0; i < series.length; i++) {
System.out.print(series[i] + " ");
}
System.out.println("");