如何在数字文字中打印/扫描下划线

时间:2014-11-19 09:15:30

标签: java-7

我无法使用Java 7 Underscores in Numeric Literals功能获取用户的输入并以与声明的格式相同的格式打印出来。请帮忙吗?或者此功能是否不完整?

Scanner input = new Scanner( System.in );
int x = 1_00_000;
System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
x = input.nextInt();    //java.util.InputMismatchException
System.out.println(x);  // Prints in normal format, but want to be in 2_00_000.

注意:在Eclipse中;我能够在运行时使用Underscored数字文字更改数字文字的值。这可能是hack,但这是在运行时rit中输入Underscored数字文字所需的功能吗?

http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html#miscellaneous

1 个答案:

答案 0 :(得分:0)

如果要维护下划线,可以使用String:

  Scanner input = new Scanner( System.in );
  System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
  String stringLiterals = input.nextLine();    
  System.out.println(stringLiterals);  // Prints 2_00_000.