Interactive Programs With Java ?


Hey! This is a first of a long series of notes about Java.

I tried to start this amazing journey with a very fundamental and kind of confidence creating lesson among java newbies .

Interactive ? What is interactive ? According to its linguistic meaning it is:

allowing a two-way flow of information between a computer and a computer-user; responding to a user’s input:

According to the meaning, interactivity gets few major stream lines.
  1. Output ( Monitor)
  2. Input (Keyboard)
Let's take a look at these topics in Java.

  1. Output ( Monitor) - How to write a program to display something in monitor. It's very simple , in fact that's kind of like the  first program we wrote in the first day of our java lecture.


                  public class outputDemo {

                                 public static void main(String[]args){

                                             System.out.println("Hello World !");

                                 }

                    }
       
      2. Input (Keyboard) - But How to give keyboard inputs to a java program. There are 3 ways.

        • Command Line Arguments
        • BufferedReader
        • Scanner
    • Now, let's see how Command Line Arguments work in java. An Example is given below.

            public class Ex01{
    
                public static void main(String[]args){
    
                    System.out.println(args[0]);
    
                    System.out.println(args[1]);
    
                }
    
            }
    



                           Your arguments should be given as  follows with space in between each Argument. They're in default gets String  data type.


    • Then, It's BufferedReader.
    •  
        import java.io.BufferedReader;
    
        import java.io.IOException;
    
        import java.io.InputStreamReader;
    
        public class Ex02 {
    
            private static String name;
    
            public static void main(String[]args) throws IOException
    
            {
    
                BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    
                System.out.println("Please Enter Your Name !");
    
                name = bf.readLine();
    
                System.out.println(name);
    
            }
    
        }
    
       

    This time it requires to import BufferedReader, IOException & InputStreamReader  classes of io package. You could use type conversion if necessary.

    InputStreamReader(System.in)  - Stands for collecting input of the keyboard byte by byte and converting them in to characters.


    BufferedReader() - Stands for collecting characters and creating a line (String Data Type).


    •  Then, let's move to Scanner . There's an example given below.
            import java.util.Scanner;
    
            public class Ex03 {
    
                public static void main(String []args)
    
                {
    
                    Scanner sc = new Scanner(System.in);
    
                          System.out.println("Please Enter a number !") ;
    
                    double number = sc.nextDouble(); 
    
                    System.out.println(number);
    
                }
    
            }
     

     The program needs to be imported Scanner Class of util package. Type conversion isn't necessary as it can be directly stored in double variable number using nextDouble() method of Scanner class.

    1 comment:

    Anonymous said...

    Great! Article! Thanks!