160 likes | 324 Views
Chapter 2: Java Fundamentals. Input and Output statements. Standard Output Window. Using System.out , we can output multiple lines of text to the standard output window. The exact style of standard output window depends on the Java tool you use. int x = 123, y = x + x;
E N D
Chapter 2: Java Fundamentals Input and Output statements
Standard Output Window • Using System.out, we can output multiple lines of text to the standard output window. • The exact style of standard output window depends on the Java tool you use. Dr. S. GANNOUNI & Dr. A. TOUIR
int x = 123, y = x + x; • System.out.println( "Hello, Dr. Caffeine.“ ); • System.out.print( " x = “ ); • System.out.println( x ); • System.out.print( " x + x = “ ); • System.out.println( y ); • System.out.println( " THE END“ ); The println Method • We use println instead of print to skip a line. Dr. S. GANNOUNI & Dr. A. TOUIR
Commonly Used Escape Sequences • Example System.out.println(“The string \”sunny\” contains five character”); The string “sunny” contains five characters Dr. S. GANNOUNI & Dr. A. TOUIR
Formatting Output with printf System.out.printf("Hello there!"); Consists of only the format string and the statement: System.out.printf("There are %.2f inches in %d centimeters.%n", centimeters / 2.54, centimeters); Consists of both the format string and argumentList. %.2f and %d are called format specifiers. By default, there is a one-to-one correspondence between format specifiers and the arguments in argumentList. The first format specifier, %.2f, is matched with the first argument, which is the expression centimeters / 2.54. 6
Formatting Output with printf The second format specifier, %d, is matched with the second argument, which is centimeters. The format specifier %n positions the insertion point at the beginning of the next line. If centimeters = 150 150/2.54 =59.05511811023 The o/p would be : There are 59.06 inches in 150 centimeters The output of a printf statement is right-justified by default. To force the output to be left-justified, negative column widths may be used. 7
Example1 public class Example3_6 { public static void main (String[] args) { int num = 763; double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890"); System.out.printf ( "%5d%7.2f%15s%n", num, x, str); System.out.printf ("%15s%6d%9.2f %n", str, num, x); } } 8
Example1 Sample run : 123456789012345678901234567890 763 658.75 Java Program. Java Program. 763 658.75 9
Example2 public class Example3_7 { public static void main (String[] args) { int num = 763; double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890"); System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str); System.out.printf("%-15s%-6d%- 9.2f ***%n", str, num, x); } } 10
Example2 Sample Run : 123456789012345678901234567890763 658.75 Java Program. ***Java Program. 763 658.75 *** 11
Standard Input • To input primitive data values, we use the Scanner class. • 4 steps are needed to be able to use input primitive: • Step 1: import the Scanner class: • import Java.util.Scanner; • Step 2 : declaring a reference variable of aScanner • Scanner read ; //we named the object read • Step 3: creating an instance of the Scanner • read = new Scanner (System.in); • Step 4: use specific methods to enter data • int x = read.nextInt(); Dr. S. GANNOUNI & Dr. A. TOUIR
Example • 1 import Java.util.Scanner; • 2 Scanner input ; // declaring the reference variable of a Scanner • 3 int area ,length, width; // declaring variables to store entries • 4 input = new Scanner (System.in); // creating an instance • 5 length = input.nextInt(); //reading the length from the keyboard • 6 width = input.nextInt(); //reading the width from the keyboard • 7 area = length * width ; // computing the area • // displaying the result • 8 System.out.println(“the legnth is ”+ lenght); • 9 System.out.println(“the width is ”+ width); • 10 System.out.println(“the area is ”+ area); Dr. S. GANNOUNI & Dr. A. TOUIR
Common Scanner Methods • Method Example Scanner input = new Scanner (System.in); nextByte( ) byte b = input.nextByte( ); nextDouble( ) double d = input.nextDouble( ); nextFloat( ) float f = input.nextFloat( ); nextInt( ) int i = input.nextInt( ); nextLong( ) long l = input.nextLong( ); nextShort( ) short s = input.nextShort( ); next() String str = input.next(); Dr. S. GANNOUNI & Dr. A. TOUIR
Input Reading a Single Character if ch is a charvariable. To input A into ch, you can use the following statement: ch = console.next().charAt(0); 16