Simple Text IO java util Scanner n n

  • Slides: 9
Download presentation
Simple Text I/O

Simple Text I/O

java. util. Scanner n n Java finally has a fairly simple way to read

java. util. Scanner n n Java finally has a fairly simple way to read input First, you must create a Scanner object n n n To read from the keyboard (System. in), do: n Scanner scanner = new Scanner(System. in); To read from a file, do: n File my. File = new File("my. File. Name. txt"); Scanner scanner = new Scanner(my. File); n You have to be prepared to handle a File. Not. Found exception You can even “read” from a String: n Scanner scanner = new Scanner(my. String); n This can be handy for parsing a string

Using the Scanner n n First, you should make sure there is something to

Using the Scanner n n First, you should make sure there is something to scan n scanner. has. Next() boolean n You wouldn’t use this when reading from the keyboard You can read a line at a time n n Or, you can read one “token” at a time n n A token is any sequence of nonwhitespace characters scanner. next () String You must be prepared to deal with exceptions n n scanner. next. Line() String Eclipse will tell you what you need to do These return Strings, which you can convert to numbers or other types if you like There also methods to check for and return primitives directly

Scanning for primitives n You can read in and convert text n And test

Scanning for primitives n You can read in and convert text n And test if you have to primitives: something to read: n n n n boolean b = sc. next. Boolean(); byte by = sc. next. Byte(); short sh = sc. next. Short(); int i = sc. next. Int(); long l = sc. next. Long(); float f = sc. next. Float(); double d = sc. next. Double(); n n n n has. Next. Boolean() has. Next. Byte() has. Next. Short() has. Next. Int() has. Next. Long() has. Next. Float() has. Next. Double()

Formatted output n System. out. println(Math. PI); will print out 3. 141592653589793 n n

Formatted output n System. out. println(Math. PI); will print out 3. 141592653589793 n n n Prior to Java 1. 5, you had to figure out how to do this yourself n n n If you want to print out this number as 3. 1416, or 3. 14, you need to format it If you want to print out numbers in neat columns, you need to format them Java 1. 5 introduced the Formatter class to do formatting for you In typical Java style, Formatter can do just about anything—but doesn’t try to make the common things easy For the most part, we won’t use the Formatter class directly, but will use System. out. format(. . . )

Formatted output n n Java 5 has a printf method, similar to that of

Formatted output n n Java 5 has a printf method, similar to that of C Each format code is % width code n The width is the number of characters that are output (with blank fill) n n n Some values for the code are s for strings, d for integers, f for floating point numbers, b for booleans n n By default, output is right-justified A negative width means to left-justify the output For floating point numbers, the width has the form total. right, where total is the total width and right is the number of digits to the right of the decimal point There a huge number of options formatting dates, which we won’t cover

Examples n System. out. printf("Left justified: System. out. printf("Right justified: System. out. format("Left justified:

Examples n System. out. printf("Left justified: System. out. printf("Right justified: System. out. format("Left justified: System. out. format("Right justified: n Left justified: Right justified: Left justified: Right justified: |abc | | abc| |25 | | 25| |3. 1416 | | 3. 1416| |3. 14 | | 3. 14| |true | | true| |%-8 s|n", "abc"); |%-8 d|n", 25); |%-8. 4 f|n", Math. PI); |%-8. 2 f|n", Math. PI); |%-8 b|n", true); |%8 b|n", true);

But wait…there’s more n n We have just scratched the surface of the Scanner

But wait…there’s more n n We have just scratched the surface of the Scanner and Formatter classes See the Java API for more details

The End

The End