public static void main(String argv[]){
String a="0700";
Scanner s = new Scanner(a);
while(s.hasNextLong()){
System.out.print(s.nextLong()+",");
}
結果は「448」ではなく「700」になります。
デフォルトでは、スキャナーは数値が基数10であると想定し、先頭の0を無視します。必要に応じて、別の基数を指定できます。以下のコードは448を出力します。
public static void main(String[] args) {
String a = "0700";
Scanner s = new Scanner(a);
while (s.hasNextLong(8)) { //make sure the number can be parsed as an octal
System.out.print(s.nextLong(8)); //read as an octal value
}
}
メソッドを使用してデフォルトの基数を設定するか、 andメソッドScanner#useRadix(radix)
に明示的に基数を渡すことができます。Scanner#hasNextLong(radix)
Scanner#nextLong(radix)
ドキュメントを読んでください。スキャナーのデフォルトの基数を使用することを示しています。デフォルトが受け入れられない場合は、useRadix(int radix)
メソッドを使用して変更するかnextLong(int radix)
、基数の一時的な変更にを使用できます。