JAVA programming language design (Elementary) notes excerpt
To avoid input errors, do not
nextByte()
,nextShort()
,nextInt()
etc. later usenextLine()
nextXXXXX()
Are referred to令牌读取方法
, they will read tokens separated by delimiters.
next()
Read a character string delimited by delimiters, andnextLine()
the read row line to a separator end.
Token reading method can not be read back of the token separator. If the token is called after reading methodnextLine()
, which is read from the start delimiter, the delimiter line end of the line of characters. The line separator is also read, but it is notnextLine()
returned string portion.For example, the following documents
34 567
Execute code 1:
Scanner input = new Scanner(new File("test.txt")); int intValue = input.nextInt(); String line = input.nextLine();
intValue
Value of 34, and theline
character contains the `,
. 5,
. 6,
7 '.If the input is entered from the keyboard (
34
carriage return):Execute code 2:
Scanner input = new Scanner(System.in); int intValue = input.nextInt(); String line = input.nextLine();
It will be
intValue
a value of 34, andline
is an empty string. The reason is that the token reading methodnextInt()
reads 34, then stops at the separator, where the separator is a line separator (Enter).nextLine()
After reading method will line separator, then return line before the string delimiters. Because there are no characters before the line separator, itline
is empty.Common escape character
\ B backspace
\ T Tab key
\ N linefeed
\ F feed
\ R Enter key
\ Backslash
\ `Single quotes
\ " Double quotes
- Common identifiers
% B Boolean value
% C character
% D decimal integer
% F float
% E standard form Scientific Notation Number
% S string
- Examples of the development width and precision
% 5c outputs a character and in front of the character entry box plus 4
% 6b Boolean output, a space in front of false, true before the two spaces
% 5d output integer entries, a width of at least 5, if the entry is less than 5 digits, the spaces. Otherwise automatically increase width.
Float% 10.2f output width of at least 10, including decimal and decimal places. Thus, before the decimal point is assigned to 7, the same space if less than the excess automatically increase the width.
% 10.2e output width floating point entry is at least 10, including decimal places and two decimal exponent part. Shortage and excess above.
% 12s output string width of at least 12. Ibid.