Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The.

Similar presentations


Presentation on theme: "1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The."— Presentation transcript:

1 1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The num you typed is " + x); Requires an import statement: You can just click and eclipse will add this for you: import java.util.Scanner;

2 Scanner class The Scanner class is a class in java.util that allows us to read values of various types We can read input from either the keyboard (as we saw) or from a file (to be seen) When reading from the keyboard, we need the parameter to be System.in We’ve been using System.out- writes to the console. System.in reads from the console (little window below the programming window in Eclipse) Like standard input (stdin) in c++

3 What the Scanner reads: The Scanner looks for tokens in the input. A token is a series of characters that ends with whitespace. a blank, a tab, a carriage return, or the end of the file. Methods for reading tokens:.nextInt().nextDouble().nextFloat().next() // next String.nextLine() And then there’s.close() MORE TO COME!

4 Example: Scanner in = new Scanner(System.in); System.out.println("Enter an int, a float, a double and a string."); System.out.println("Separate each with a blank or return."); int x1 = in.nextInt(); float f1 = in.nextFloat(); double d1 = in.nextDouble(); String s1 = in.nextLine(); System.out.println("Now enter another value."); String s2 = in.next(); System.out.println("Here is what you entered: "); System.out.println(x1 + " " + f1 + " " + d1 + " " + s1 + " and " + s2); in.close(); Enter an int, a float, a double and a string. Separate each with a blank or return. 2 3.1 3.24 hi there how are you Now enter another value. no Here is what you entered: 2 3.1 3.24 hi there how are you and no

5 Processing Files File class contains a reference to an actual file location on your machine Special classes can read/write data to the file Scanner can actually read a file just like console input But we have to make a File object from the file class first: File myFile = new File(“myFile.txt”); //myFile.txt is the name of the file on your computer. Scanner in = new Scanner(myFile); if myFile.exists() //.exists is a method that checks to see if the myFile object was successfully created. (Why wouldn’t it have been?) What must it return? You can now use in to read from myFile.txt just like you read from the keyboard: int x1 = in.nextInt(); float f1 = in.nextFloat(); double d1 = in.nextDouble(); String s1 = in.nextLine(); String s2 = in.next();

6 Scanning in a file: Now we may want to use a few other scanner class methods:.hasNext() // returns true if there is more data to be read.hasNextInt() //returns true if the next value is an int.hasNextFloat() // returns true if the next value is a float.hasNextDouble() // returns true if the next value is a double We’ll likely use the.hasNext method (how?)

7 int[][] matrix = new int[][]; File fl = new File(filename); if (fl.exists()) { System.out.println("The file exists!"); } Scanner fn = new Scanner(fl); int row = 0; while (fn.hasNext()) { String line = fn.nextLine(); System.out.println(line); String[] numarr = line.split("\t"); for (int i = 0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4172826/slides/slide_7.jpg", "name": "int[][] matrix = new int[][]; File fl = new File(filename); if (fl.exists()) { System.out.println( The file exists! ); } Scanner fn = new Scanner(fl); int row = 0; while (fn.hasNext()) { String line = fn.nextLine(); System.out.println(line); String[] numarr = line.split( \t ); for (int i = 0; i

8 Exceptions What could go wrong when we are trying to write data to a file? out of disk space don't have permission to write disk disconnected during our write file "locked" by a different program (maybe another program is writing to it when you want to…) etc

9 Exceptions We want to check for some exceptions, especially if we know it’s something that occurs regularly. Certain methods in java create exceptions for us They “throw an exception” File IO (the Scanner class) definitely creates exceptions for us. Some of the file IO exceptions are: IOException EOFException FileNotFoundException

10 Try…Catch We use Try…Catch to catch exceptions that are thrown. So if a method might “throw an exception” if something goes wrong, we can try it. If we don’t succeed and an exception is thrown, we “catch it. E.g.,: File fl = new File(filename); try { Scanner fn = new Scanner(fl); int row = 0; while (fn.hasNext()) { String line = fn.nextLine(); arr[row] = line; row++; } fn.close(); } catch (FileNotFoundException e) { System.out.println(“e.getMessage()"); System.exit(0); }

11 You can catch more than one exception: private int[][] makeMatrix(String filename) { int[][] matrix = new int[13][13]; File fl = new File(filename); try { Scanner fn = new Scanner(fl); int row = 0; while (fn.hasNext()) { String line = fn.nextLine(); System.out.println(line); String[] numarr = line.split("\t"); for (int i = 0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4172826/slides/slide_11.jpg", "name": "You can catch more than one exception: private int[][] makeMatrix(String filename) { int[][] matrix = new int[13][13]; File fl = new File(filename); try { Scanner fn = new Scanner(fl); int row = 0; while (fn.hasNext()) { String line = fn.nextLine(); System.out.println(line); String[] numarr = line.split( \t ); for (int i = 0; i

12 In general: There’s Exception – the catch-all exception that’s thrown. To find out what the problem was, use the Exception object’s getMessage method. E.g., try { int x = 10; int y = 0; int z = x/y; System.out.println(z); } catch (Exception err) { System.out.println(err.getMessage()); } Google other Exception errors if you want to use them.

13 Writing to a file: System.out.println is actually a special instance of PrintStream File myOutputFile = new File(“myOutputFile.txt”); PrintStream output = new PrintStream(myOutputFile); PrintStream “throws an error” so we will want to try it and, if unsuccessful, catch the error.

14 For example: public void makefile() { File f1 = new File("testout.txt"); try { PrintStream outfile = new PrintStream(f1); for (double[] x: newmatrix) { for (double y: x) { outfile.print(y + " "); } outfile.println(); } outfile.close(); } catch(IOException io) { System.out.println(io.getMessage()); }

15 How about formatting? In the previous example we were printing out doubles generated randomly. We get: 217.8575927137373 597.655625825856 210.56479046932353 518.4918672220385 980.1225356891215 249.22772143775117 94.27214196883537 218.54436204437766 909.1938505252127 64.77187265528717 522.4332161890065 409.3104756268032 523.8845868006724 545.9574555472581 480.24527204720766 423.3817954814687 1.1995922112015833 31.542425381440474 159.2409972487274 826.9895092890089 876.7185709820699 905.5090060305496 826.2549887160317 902.8826404817949 102.6001484498129 865.678399786176 869.3623864174282 764.6236242191251 832.6226040740607 744.5345466483448 480.5374374958481 328.8940102976276 804.6528435755249 476.5255281033308 701.3804741693555 865.5677865881347 473.16200063038025 939.9005125578615 106.11401024206168 869.7228980147195 107.8829700207542 566.1791646848557 225.14981932646862 555.4267916885535 919.4309107470867 806.6396061869323 922.8806070647845 358.04479313158765 898.6853144841515 772.7498589630353 334.9514250847454 449.98473057812527 463.9578866182207 626.2568492194717 643.117385541904 424.2054922717532 198.44775061227148 584.6863387866528 919.8961924374581 980.4804996320166 930.2115601909552 729.2957731241569 510.5749895400613 661.322074457543 157.8530590786883 155.06158864366725 936.7331953500385 427.63591389063413 357.3626766233403 386.07120201134194 103.29393913692742 138.5895515411516 19.015139669919414 394.72893290349253 670.2189767548267 805.8424225713404 357.9585978625832 9.474846909941048 810.3128833868223 829.1992908814386 518.1060966371921 973.2535478211257 216.2041575662601 288.9128758724809 746.1667967184507 423.75974056620225 142.42259858874107 769.2755643999647 451.13010115309584 106.52640929375889 472.5935354330558 118.80427438277653 284.2434781014766 203.21002242875142 287.12133694128306 14.23626895067953 375.5006089897035 259.96665890567016 173.33597287852288 188.71818119248795 644.883318809338 334.60225938402357 567.9282666825169 708.9843882036027 670.0770831263618 146.21828511551684 750.6004229089104 741.984720568829 937.1070809292727 707.3644617291698 718.2110183812108 838.3361590205222 426.3172853136753 646.248048345563 60.29399686015824 878.1364931147457 This is ugly, and we often don’t care about the.00000001th place. Can we format?

16 Formatting: Instead of using print, use format, e.g, System.out.format(“”); Outfile.format(); And then we format, e.g.,: Double pi = 3.14159; System.out.format(“%3.2f”, pi); Prints: 3.14 %f prints a double or float out to 6 places %.3f prints a double or float with 3 values after the decimal (but as many numbers as needed) %4.3f prints a double with 3 values after the decimal but the whole string occupying 4 places. It doesn’t truncate the number before the decimal. However, if the number is smaller than the first number (e.g., %10.3), it will make the whole number 10 characters long, including the decimal, then add spaces to the left.

17 More formatting: Integer formatting: %d prints an integer with as many digits as needed %4d prints an integer with as many digits as needed, but always at least 4, with spaces to the left %04d prints an integer with as many digits as needed, but always at least 4, with zeros to the left Examples: System.out.format(“%d, %4d, %04d”, 34291, 34, 34); Prints: 34291, 34, 0034

18 Formatting Strings: %s prints the string (the whole string) %15s prints out a string with the specified number, right justified. %-15s prints out the string with the specified number, left-justified E.g., System.out.format("%s: %15s: %-15s:", "echidna", "wombat", "puffin"); Prints: echidna: wombat: puffin :


Download ppt "1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The."

Similar presentations


Ads by Google