Search This Blog

How to take input in Java?:-







Java provides java.lang package and System class to take input from User's and display Output on an output device.



                    java.lang   (package)

                     Object     (Super Class)

                     System      (Sub Class)


PrintStream(It is used to manage output operation)  InputStream(It is used to manage input operation)

print()                                                                         read()
println()


PrintStream out;

out is the reference variable of PrintStream class which is used to access print() or println()
System.out.print()
System.out.println();


InputStream in;

in is the reference of InputStream class which will be used to call read() for input operation


System.in.read();


but read() return byte data and throw exception IOException hence System.in reference will be managed by the Scanner class which will store input data in an Object pattern.


Scanner sc = new Scanner(System.in);


Scanner class provides a predefined method to convert object type to another data type of Java.



int a = sc.nextInt()  //Scanner object data to integer

float b = sc.nextFloat()  //float

double d = sc.nextDouble()  //double

String s = sc.next();   //string

String s1 = sc.nextLine();  //paragraph





Example of System.in.read()?

import java.io.*;
class InputDemo
{
   public static void main(String args[]) throws IOException
   {
       int a = System.in.read();
       System.out.println((char)a);

    }


}



...................................................................................
import java.util.Scanner;
class InputDemo
{
   public static void main(String args[])
   {
     
       Scanner sc = new Scanner(System.in);   // System.in is mandatory to pass under Scanner Objectg
       System.out.println("enter first number");
       int a = sc.nextInt();
       System.out.println("enter second number");
       int b = sc.nextInt();
       System.out.println(a+b);

    }


}



......................................................................................................................................................................


Using Command Prompt:-

The command prompt is a very old approach to take input from users, this is used on command-line interface based software.


Java provides a parametrized String array to handle command-line input.



public static void main(String args[])
{




}

 


Create program:-

class InputExample2
{

   public static void main(String args[])
   {
        String s = args[0];
        String s1 = args[1];
        System.out.println("value is "+s + " value is "+s1);


   }




}


Compile

javac InputExample2.java


java InputExample2 "hello" "12.345"



Program of addition using Command Prompt Input

class InputExample2
{

   public static void main(String args[])
   {
        int s = Integer.parseInt(args[0]);
        int s1 =Integer.parseInt(args[1]);
        System.out.println(s+s1);


   }




}







1 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post