Unformatted text preview:

Simple Text I O Jan 14 2019 java util Scanner Java finally has a fairly simple way to read input First you must create a Scanner object To read from the keyboard System in do Scanner scanner new Scanner System in To read from a file do File myFile new File myFileName txt Scanner scanner new Scanner myFile You have to be prepared to handle a FileNotFound exception You can even read from a String Scanner scanner new Scanner myString This can be handy for parsing a string Using the Scanner First you should make sure there is something to scan scanner hasNext boolean You wouldn t use this when reading from the keyboard You can read a line at a time Or you can read one token at a time A token is any sequence of nonwhitespace characters scanner next String You must be prepared to deal with exceptions scanner nextLine 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 are also methods to check for and return primitives directly Scanning for primitives You can read in and convert text And test if you have to primitives something to read boolean b sc nextBoolean byte by sc nextByte short sh sc nextShort int i sc nextInt long l sc nextLong float f sc nextFloat double d sc nextDouble hasNextBoolean hasNextByte hasNextShort hasNextInt hasNextLong hasNextFloat hasNextDouble Formatted output Java 5 has a printf method similar to that of C Each format code is width code Some format codes are s for strings d for integers f for floating point numbers Example double pi Math PI System out printf 8s 8s 6d 6d 8f 8 2f n abc def 123 456 pi pi System out printf 8s 8s 6d 6d abcdef ghijkl 12345 6789 Output abc def abcdef ghijkl 123 456 12345 6789 3 141593 3 14 But wait there s more We have just scratched the surface of the Scanner and Formatter classes See the Java API for more details The End


View Full Document

Penn CIT 591 - Simple Text

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view Simple Text and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Simple Text and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?