inttime[]
是一個已經存儲了一位數的整數值的數組。第一個for
循環試圖將每個整數轉換爲二進制字符串並將其存儲到字符串數組bintime[]
。這部分工作正常,但是當我嘗試運行下一個for循環時,出現非法格式轉換錯誤。爲什麼我不能使用String.format()來格式化來自Integer.toBinaryString()的字符串?
for(int j = 0; j < inttime.length; j++){
bintime[j] = Integer.toBinaryString(inttime[j]);
}
for(int a = 0; a < bintime.length; a++){
System.out.println(String.format("%04d",bintime[a]));
}
這究竟是爲什麼,如果bintime[]
是String
陣列?
'%04d'需要一個整數(因此'd')。使用'%04s'。 – oldrinb
您的問題可以解答您的問題 - 爲什麼您不能將字符串格式化爲數字?因爲他們是字符串。 –
您可以使用System.out.format作爲快捷方式。 – maba