DOC PREVIEW
Penn CIT 591 - Simple Text

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Simple Text I/Ojava.util.ScannerUsing the ScannerScanning for primitivesFormatted outputSlide 6ExamplesBut wait…there’s moreThe EndJan 14, 2019Simple Text I/Ojava.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 stringUsing 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scanner.nextLine()  String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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 directlyScanning for primitivesYou can read in and convert text to primitives: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();And test if you have something to read:hasNextBoolean()hasNextByte()hasNextShort()hasNextInt()hasNextLong()hasNextFloat()hasNextDouble()Formatted outputSystem.out.println(Math.PI); will print out3.141592653589793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Prior to Java 1.5, you had to figure out how to do this yourself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Java 5 has a printf method, similar to that of CEach format code is % width codeThe width is the number of characters that are output (with blank fill)By default, output is right-justifiedA negative width means to left-justify the outputSome values for the code are s for strings, d for integers, f for floating point numbers, b for booleans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 are a huge number of options for formatting dates, which we won’t coverExamplesSystem.out.printf("Left justified: |%-8s|\n", "abc");System.out.printf("Right justified: |%8s|\n", "abc");System.out.printf("Left justified: |%-8d|\n", 25);System.out.printf("Right justified: |%8d|\n", 25);System.out.printf("Left justified: |%-8.4f|\n", Math.PI);System.out.printf("Right justified: |%8.4f|\n", Math.PI);System.out.format("Left justified: |%-8.2f|\n", Math.PI);System.out.format("Right justified: |%8.2f|\n", Math.PI);System.out.format("Left justified: |%-8b|\n", true);System.out.format("Right justified: |%8b|\n", true);Left justified: |abc |Right justified: | abc|Left justified: |25 |Right justified: | 25|Left justified: |3.1416 |Right justified: | 3.1416|Left justified: |3.14 |Right justified: | 3.14|Left justified: |true |Right justified: | true|But wait…there’s moreWe have just scratched the surface of the Scanner and Formatter classesSee the Java API for more detailsThe


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
Download Simple Text
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?