[英]My program doesn't print out Strings converted to ints
我有一個簡單的程序,要求您通過鍵盤輸入數字(每個數字用空格或逗號分隔),然后將它們從低到高排序並打印出來。
問題是數字沒有打印出來。
這是源代碼:
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
}
}
編輯:添加了a++;
表達式在第26行阻止程序進入無限循環和新的if
塊阻止程序“凍結”,但現在我收到此錯誤:
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
我正在通過提供一種有效的替代方案來回答:
System.out.println( Arrays.stream(input.split("\\D+"))
.map(Integer::parseInt)
.sorted()
.map(String::valueOf)
.collect(Collectors.joining(", ", "", ".")));
它只有1行。
根據大眾需求,以下是它的工作原理:
input.split("\\\\D+")
返回數字的String[]
,用非數字分隔 - \\D
表示“非數字”, \\D+
表示一個或多個非數字 Arrays.stream()
從數組創建流 map(Integer::parseInt)
將每個String
從拆分轉換為Integer
。 這是必要的,所以下一步將按數字順序排序,而不是排序順序 - nb 10 > 2
但是"10" < "2"
sorted()
對整數流進行sorted()
數字順序排列) map(String::valueOf)
將Integers
轉回Strings
,為下一步做好准備 collect()
將流組合到單個對象。 請參閱下一點了解...... Collectors.joining(", ", "", ".")
是一個預先固定的收集器,它從流中生成一個String,將它們與參數1作為分隔符,參數2的前綴和參數3的后綴連接起來 當你的程序讀取第一個“,”時,
它將增加“i”,但“a”不會增加,所以,
input.charAt(a)將為','且numString [i]為null
因此,將跳過if條件並導致無限循環。
//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(",");
這是代碼示例,該數字將打印出來。 我修改了你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] + ".");
}
}
}
試試這個代碼。 它只是做這項工作。 您可以從逗號或空格分隔輸入。 但不是他們倆。
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("");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.