为什么java.util.Scanner不能将0700视为八进制文字?

Edd*_*ion 0 java octal

   public static void main(String argv[]){
         String a="0700";
         Scanner s = new Scanner(a);
         while(s.hasNextLong()){
              System.out.print(s.nextLong()+",");
         }
Run Code Online (Sandbox Code Playgroud)

结果将是"700",而不是"448".

ass*_*ias 5

默认情况下,扫描程序假定该数字位于基数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
    }
}
Run Code Online (Sandbox Code Playgroud)