Page 254 - PowerPoint Presentation
P. 254

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               Getting  Input  from  the  Keyboard  using  Scanner  Class,  BufferedReader  and
               InputStreamReader Class, and JOptopionPane Class.


               Java Scanner
                       Java Scanner comes under the java.util package. Java has various ways to read input
               from the keyboard, the java.util.Scanner is one of them.

                  //EXAMPLE OF java.util.Scanner PACKAGE

                  import java.util.Scanner;          //import the java.uilt package

                  public class Operation{
                  public static void main(String[] args){


                  Scanner sc = new Scanner(System.in);  // declaring the “sc” as the new scanner
                                              // sc is just like a variable name. it can be change.
                                              // (System.in) is a standard input source for getting input from
                                                     console or terminal. It’s a global access object.

                  int num1, num2, sum; difference, product, quotient, modulo;

                  System.out.println(“Enter the First Number: “);
                  num1 = sc.nextInt();   // here ‘sc’ is applied. Because ‘sc’ is declared as the new scanner
                  System.out.println(“Enter the Second Number: “);
                  num2 = sc.nextInt();

                  sum = num1+num2;
                  difference = num1-num2;
                  product = num1*num2;

                  quotient = num1/num2;
                  modulo = num1%num2;

                  System.out.println(“Sum: “+sum);          // calling the sum variable using (+) sign then the
                  variable name
                  System.out.println(“Difference: “+difference);  // calling the difference variable. ‘’
                  System.out.println(“Product: “+product);   // calling the product variable.  ‘’
                  System.out.println(“Quotient: “+quotient);   // calling the quotient variable.  ‘’
                  System.out.println(“Modulo: “+modulo);    // calling the modulo variable.  ‘’
                  }
                  }

                   NOTE:
                               // example: set the scanner_name as ‘sc’.

                 string name = sc.next() or sc.nextLine();   - to read string data type.
                 int num = sc.nextInt();                    - to read integer data type
                 char a = sc.next().charAt(0);              - to read a single character.
                 float f = sc.nextFloat();                  - for float data type.
                 double d = sc.nextDouble();                - for double data type.




                                                                                                            30
   249   250   251   252   253   254   255   256   257   258   259