現在のコードで数値書式例外が発生しています。私は問題が何であるか正確にはわかりませんが、私はそれがかなりマイナーなものだと感じていますので、私のコードが "やるべきこと"について細かく説明するつもりはありませんが、とにかく指示することができます。java.lang.NumberFormatExceptionに関する問題
以下のコードでは、選択肢の並べ替えに乱数の束を使用することを意図しています。私はフォーマットの問題は、エラーメッセージ内の整数を囲む[]と関係があるかもしれないと信じていますが、私は本当に考えていません。エラーメッセージは、ボタンの下の行に戻ります。 int array [] = {Integer.parseInt(list.toString())}; これは宣言なので意味がありますが、この問題が引き続き起こらないようにする方法がわかりません。
何か入力していただきありがとうございます。明白な間違いがありましたらお詫び申し上げます。私は非常に初心者です。ここで
は、これまでのところ、私の現在のコードの本体である:
ArrayList<Integer> list = new ArrayList<Integer>();
public static void selectionSort(int[] a) {
for(int i = 0; i < a.length; i++) {
int smallestValue = a[i];
int smallestIndex = i;
for(int j = i+1; j < a.length; j++) {
if (smallestValue > a[j]) {
smallestValue = a[j];
smallestIndex = j;
}
}
a[smallestIndex] = a[i];
a[i] = smallestValue;
}
}
public void start() {
int times = Integer.parseInt(n2sInput.getText());
for (int i = 0; i < times; i++) {
list.add(new Random().nextInt(2000) - 1000);
}
}
public static String arrayToString(int[] a) {
String result = "";
for (int v : a) {
result += v + " ";
}
return result;
}
private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(selButton.isSelected()) {
start();
int array[] = {Integer.parseInt(list.toString())};
onOutput.setText(arrayToString(array));
selectionSort(array);
snOutput.setText(arrayToString(array));
} else if(bubButton.isSelected()) {
} else if(insButton.isSelected()) {
} else if(quiButton.isSelected()) {
}
}
一覧はオブジェクトであり、単純に文字列に変換することはできません。リスト全体を繰り返し処理して整数を取得しなければなりません。そうすれば、あなたは何でもできます。 – Eymen
'Integer.parseInt(list.toString())'これはリスト全体を単一の整数に解析しようとします。これは問題の原因となる可能性があります。あなたはこれを(シングルint)のようにしたいですか?または 'string'リストの各要素を' int'配列に入れたいのですか? @ Natecurt3030 –