180 likes | 371 Views
Java 5 Basics. Dr. Gita Williams. Topics. Reading Keyboard Input with the Scanner class Writing Console Output with System .out. printf () Commenting Java Source Code. Reading Keyboard Input. System.in is an object that is used to read input from the keyboard.
E N D
Java 5 Basics Dr. Gita Williams
Topics • Reading Keyboard Input with the Scanner class • Writing Console Output with System.out.printf() • Commenting Java Source Code
Reading Keyboard Input System.in is an object that is used to read input from the keyboard. Problem: The System.in reads input as byte values not int or double or String • int: 4 bytes,double: 8 bytes Solution: Use the Scanner class to read input from System.in and retrieve the input formatted as primitive values or strings.
Using the Scanner Class • Import the Scanner class from the java.util package import java.util.Scanner; • Create a Scanner object and connect it to System.in Scanner keyboard = new Scanner(System.in); • Use a Scanner method to retrieve formatted input String name; name = keyboard.nextLine();
Methods in Scanner The Scanner class has methods for reading Strings and primitive data types nextDouble() nextFloat() nextInt() nextLine() // Returns a string nextLong() nextShort()
Example: Reading Keyboard Input importjava.util.Scanner; : : { intgrade; Scanner keyboard = new Scanner (System.in); System.out.print(“Enter your grade: “); grade = keyboard.nextInt(); : }
Reading a Character To read a single character from the keyboard 1. Use Scanner’s nextLine method to read a String String input; input = keyboard.nextLine(); 2. Use String’s chartAt method to extract 1st character char answer; answer = input.charAt(0);
Mixing Calls to nextLine with calls to Other Scanner Methods • Read Pg 96-97 • Pressing [ENTER] causes a new line character to be stored in the keyboard buffer • All methods except nextLine() leave the new line character in the keyboard buffer. • When reading a String after reading primitive data, remove the leftover newline character from the buffer with an extra call to the nextLine() method
Formatted Console Output System.out.printf( FormatString, ArgumentList) • FormatString • A string that contains text and/or special formatting specifiers • AgurmentList: • A list arguments that will be formatted according to the format string. int hours = 40; double rate =13.2; double pay = hours * rate; System.out.printf( “hours: %d \t rate: %f \npay = $%.2f”, hours, rate, pay); hours: 40 rate: 13.200000 pay = $528.00
Formatted String • Escape Sequences: \n new line \t tab • Special Formatting Specifiers (d, f, s) %d integer %6d integer in a field width of 6 places (right-justified) %f floating point (1234.560000) %25f floating point in a field width of 25 places %.2f 2 digits after decimal (1234.56) %8.2 field width of 8 and 2 digits after decimal %,.2f Use commas to group digits (1,234.56) %s string %5s string in a field width of 6 places (right-justified) Read Chapter 3.11
Example: Formatted Output int num = 12; double amt =1234.56; String name ="Amy"; System.out.printf("[%d][%10d]\n",num, num); System.out.printf("[%f] [%.2f] [%10.2f]\n", amt, amt, amt); System.out.printf("[%s] [%10s]\n", name, name); [12] [ 12] [1234.560000] [1234.56] [ 1234.56] [Amy] [ Amy]
Using DecimalFormat You are still able to use DecimalFormat to format numbers. • import java.text.DecimalFormat; • Create a DecimalFormat Object and specify the format string DecimalFormat formatter = new DecimalFormat(“#0.00”); • Call the format method double num = 12.92929; System.out.println (formatter.format(num)); • Format Strings #,##0.00 Use commas, leading 0, 2 digits after decimal #0% number to be multiplied by 100 and % character
Commenting Code • Java provides three methods for commenting code. Chapter 2.11 (Page 83)
Commenting Code • Javadoc comments can be built into HTML documentation. • Comment3.java is an example. • To create the documentation: • Run the javadoc program with the source file as an argument • Ex: javadoc Comment3.java • The javadoc program will create index.html and several other documentation files in the same directory as the input file.
Commenting Code • Example index.html:
Write comments that appear just before the methods definition After the general description of a method, provide a description of each parameter by using a @param tag or @return tag @paramparameterNameDescription @return Description The description can span several lines, ending at the end of the comment */ or at the beginning of another tag Use Documentation Comments with Methods Pg 262, 274, 280
Example: Commenting Methods /** The sum method returns the sum of its two parameters. @param num1 The first number to be added. @param num2 The second number to be added. @return The sum of num1 and num2. */ publicstaticint sum (int num1, int num2) { int result; // result is a local variable // Assign the value of num1 + num2 to result. result = num1 + num2; // Return the value in the result variable. return result; } PG 281