Simple Text I/Ojava.util.ScannerUsing the ScannerScanning for primitivesFormatted outputSlide 6ExamplesBut wait…there’s moreThe EndJan 14, 2019Simple Text I/Ojava.util.ScannerJava finally has a fairly simple way to read inputFirst, you must create a Scanner objectTo 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 exceptionYou can even “read” from a String:Scanner scanner = new Scanner(myString);This can be handy for parsing a stringUsing the ScannerFirst, you should make sure there is something to scanscanner.hasNext() booleanYou wouldn’t use this when reading from the keyboardYou can read a line at a timescanner.nextLine() StringOr, you can read one “token” at a timeA token is any sequence of nonwhitespace charactersscanner.next () StringYou must be prepared to deal with exceptionsEclipse will tell you what you need to doThese return Strings, which you can convert to numbers or other types if you likeThere are also methods to check for and return primitives directlyScanning for primitivesYou 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 outputSystem.out.println(Math.PI); will print out3.141592653589793If you want to print out this number as 3.1416, or 3.14, you need to format itIf you want to print out numbers in neat columns, you need to format themPrior to Java 1.5, you had to figure out how to do this yourselfJava 1.5 introduced the Formatter class to do formatting for youIn typical Java style, Formatter can do just about anything—but doesn’t try to make the common things easyFor the most part, we won’t use the Formatter class directly, but will use System.out.format(...)Formatted outputJava 5 has a printf method, similar to that of CEach format code is % width codeThe width is the number of characters that are output (with blank fill)By default, output is right-justifiedA negative width means to left-justify the outputSome values for the code are s for strings, d for integers, f for floating point numbers, b for booleansFor 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 pointThere are a huge number of options for formatting dates, which we won’t coverExamplesSystem.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 moreWe have just scratched the surface of the Scanner and Formatter classesSee the Java API for more detailsThe
View Full Document