2016-03-30 11 views
-4

キーボードで数字を入力するか(各数字をスペースまたはカンマで区切って)、低い順に並べ替えるという簡単なプログラムを作成したかったのです。System.out.printが機能しない

キーボードで数値を入力し、入力をStringオブジェクトに設定し、文字が数字か通常の文字かどうかを判別するforループにStringを渡すことをユーザーに求めていました。 caracterが数字の場合、String配列のフィールドに追加され、次の数字を探し、1文字が数字になるまで同じことを行います。この場合、別の数字が検索され、次の文字列配列のフィールドと同じプロセスが繰り返されます。プログラムがホール文字列の長さを通過すると、文字列配列の各フィールドはintに変換され、ソートしてから印刷することができます。

問題は、ソートされた数値を印刷できないことです。 乱数を入力するように頼み、ソートしますが、結果は表示されません。ここで

は、ソースコードは次のとおりです。

import java.util.Scanner; 

public class StartHere { 
public static void main(String[] args) { 

    Scanner scanner = new Scanner(System.in); 
    System.out.print("Type random numbers: "); 
    String input = scanner.nextLine(); 
    String[] numString = new String[input.length()]; 
    int[] numbers = new int[numString.length]; 
    int a = 0; 
    int i = 0; 

    for (i = 0; i < input.length(); i++){ // Each numString field is initialized as " ". 
     numString[i] = ""; 
    } 

    i = 0; 

    while(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)); // The char is appended to the numString so the hole String field can be 
                   // converted to an integer later. 
      a++; // We go to the next character. 
     } 
     if ((numString[i] != null) && (!Character.isDigit(input.charAt(a)))) { // If the numString[i] field is occupped and the character 
                       // at input[a] is not a digit. 
      i++; // We go to the next field. 
     } 
    } 

    for (int b = 0; b < numString.length;) { // Inside this for loop each field of the numString array 
              // is converted to an integer. 
     if (numString[b] != null) { 
      numbers[b] = Integer.parseInt(numString[b]); 
      b++; 
     } else { 
      b++; 
     } 
    } 

    quickSort(numbers, 0, numbers.length - 1); // Sorts the numbers from smaller to higher. 

    for (int c = 0; c < numbers.length - 1; c++){ // 
     if(c != numbers.length - 1){ 
      System.out.print(numbers[0] + ", "); 
     }else System.out.println(numbers[c] + "."); 
    } 
} 

public static void quickSort(int[] numbers, int left, int right) { // Defines the quickSort method. 
    int pivot = numbers[left]; // We take the first element as pivot. 
    int l = left; // Searches from left to right. 
    int r = right; // Searches from right to left. 
    int aux; 

    while (l < r) { // While searches are not 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 this have not been crossed. 
      aux = numbers[l]; // Interchange. 
      numbers[l] = numbers[r]; 
      numbers[r] = aux; 
     } 
    } 
    numbers[left] = numbers[r]; 
    numbers[r] = pivot; 
    if (left < r - 1) { 
     quickSort(numbers, left, right - 1); 
    } 
    if (r + 1 < right) { 
     quickSort(numbers, r + 1, right); 
    } 
} } 

私は何ができますか?前もって感謝します!

編集:今、私はString[] numStringの各フィールドを初期化し、これは、コンソールが言うことである:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at StartHere.main(StartHere.java:34) 
+1

System.out.print(numbers [0] + "、") - 数字であることを意味していることを確認してください[ –

+2

] "私は既に私のプログラムが前の質問で何をすべきか説明しました。それに "それぞれの問題をスタンドアロンにし、この質問に関連するコードが含まれている[mcve]に質問内のコードを減らしてください。 –

+0

Jon Skeet:なぜこの問題があるのか​​わからないので、コードのどの部分が関係するのか分かりません。 とにかく、最初のヒントをありがとう! – SpaceCore186

答えて

2

あなたが最初n-1回の最初の番号を印刷している:

if(c != numbers.length - 1){ 
    System.out.print(numbers[0] + ", "); <-- this 
}else System.out.println(numbers[c] + "."); 

あなたはそれをnumbers[c]に変更する必要があります。

2

この方法には本当に問題があります:

while(i < (input.length() - 1)) { 
    if (Character.isDigit(input.charAt(a))) { 
     numString[i] += Character.toString(input.charAt(a)); 
     a++; // We go to the next character. 
    } 
    if ((numString[i] != null) && (!Character.isDigit(input.charAt(a)))) { 
     i++; // We go to the next field. 
    } 
} 

以来、あなたはnumString[i] += Character.toString(input.charAt(a));入力6だけでなく6のためにあなたにnull6ような何かを与えることString配列numStringを、初期化したことがありません。

numbers[b] = Integer.parseInt(numString[b]);を実行して、後でそれをIntegerに解析しようとしています。これで例外がスローされます!

また、ループ条件を再評価する必要があります。 input.length()while(i < (input.length() - 1))aも減算してはいけませんか?