Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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()       // display output without new line char \n

System.out.println();  // display output with new line char  \n 


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);

note:-  Scanner is not used to take input, it will only manage input data of System.in reference hence Scanner is also called Input Manager Class.


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);

    }


}



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


Scanner class Example to manage integer and char input operation:-


import java.util.*;
class InputDemo
{
    public static void main(String scs[])
    {
        Scanner st = new Scanner(System.in);
        System.out.println("Enter int");
        int a1 =st.nextInt();
        System.out.println(a1);
        System.out.println("Enter char");
        char a2 =st.next().charAt(0);
        System.out.println(a2);

    }

}


Another Example of Scanner Class in Java:-


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);

    }


}


Scanner class Example with all datatype:-

import java.util.Scanner;
class AllInput
{
   public static void main(String args[])
   {
          int a;
          char ch;
          String s;
          float f;
          double d;
          Object o;
          Integer i;
          Float f1;
          Scanner sc = new Scanner(System.in);
          System.out.println("enter int, char, string, float, double, object, 

Integer, Float ");
         a = sc.nextInt();
         ch = sc.next().charAt(0);
         s = sc.next();
         f = sc.nextFloat();
         d = sc.nextDouble();
         o = sc.next();
         i = sc.nextInt();
         f1 = sc.nextFloat();
         System.out.println("a = "+a + "ch = "+ch + "s = "+s + "f = "+f + "d="+d+" 

o="+o+" i="+i + "f="+f1);
 
          


   }


}
......................................................................................................................................................................


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.

DOS is the best example of command-line input,  we can manage all DOS operations using a set of commands.


Java provides a parametrized String array under main() to handle command-line input.

Java can not create multiple types of main() hence it has created a single main() to perform all tasks.



public static void main(String args[])   //here args[] is the string array
{




}

 


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);


   }




}


Another Example of Command-Line input in java:-

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

       // System.out.println(args[0] + "," + args[1] + " ," + args[2]);

        for(String s:args)
        {
            System.out.println(s);
        }   

    }



}

java provides Wrapper class to convert the primitive data type to object type.

Integer, Float, Double, Character, Boolean is the Wrapper class of Java.


parseInt() method to convert Object to Integer  or String to integer using Integer.parseInt()

parseFloat() method to convert Object to float or String to float using Float.parseFloat().



1)  WAP to calculate simple interest using Command Line Input?

2)  WAP to calculate the area of Triangle using Command Line Input?

3) WAP to calculate Area of Circle using Command Line Input?

4)  WAP to calculate the Swap program using Command Line Input?

5)  WAP to calculate Square and Cube using Command Line Input?



                                            


                         






56 Comments

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

  1. explained very well, thank you sir

    ReplyDelete
  2. class Sis
    {
    public static void main(String args[])
    {
    int p =Integer.parseInt(args[500]);
    int t =Integer.parseInt(args[2]);
    int r =Integer.parseInt(args[5]);
    System.out.println((p*t*r)/100);
    }
    }

    ReplyDelete
  3. class Sis
    {
    public static void main(String args[])
    {
    int p =Integer.parseInt(args[0]);
    int t =Integer.parseInt(args[1]);
    int r =Integer.parseInt(args[2]);
    System.out.println((p*t*r)/100);
    }
    }

    ReplyDelete
  4. class Triangle
    {
    public static void main(String args[])
    {
    int b=Integer.parseInt(args[0]);
    int h=Integer.parseInt(args[1]);
    System.out.println((b*h)/2);
    }
    }

    ReplyDelete
  5. class Centigrade
    {
    public static void main(String args[])
    {
    int f=Integer.parseInt(args[0]);
    System.out.println("c=" + (5*f-160)/9);
    }
    }

    ReplyDelete
  6. class Celcius
    {
    public static void main(String args[])
    {
    int f=Integer.parseInt(args[0]);
    System.out.println("c=" + (5*f-160)/9);
    }
    }

    ReplyDelete
  7. class Fahrenite
    {
    public static void main(String args[])
    {
    int c=Integer.parseInt(args[0]);
    System.out.println("F=" + ((c*9/5)+32));
    }
    }

    ReplyDelete
  8. // WAP to calculate simple interest
    import java.util.Scanner;
    class Sii
    {
    public static void main(String[] args)
    {
    Scanner h = new Scanner(System.in);
    System.out.println("Enter principal amount");
    int p = h.nextInt();
    System.out.println("Enter rate of interest");
    float r = h.nextFloat();
    System.out.println("Enter time duration (in year)");
    float t = h.nextFloat();
    float si = (p*r*t)/100;
    System.out.println("Simple Interest is = "+si);
    }
    }

    ReplyDelete
  9. // WAP to calculate area of triangle
    import java.util.Scanner;
    class Artriangle
    {
    public static void main(String...area)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter base of triangle");
    int b = sc.nextInt();
    System.out.println("Enter height of triangle");
    int h = sc.nextInt();
    float ar = 0.5F*(b*h);
    System.out.println("Area of triangle is = "+ar);
    }
    }

    ReplyDelete
  10. //WAP to calculate area of circle
    import java.util.Scanner;
    class Arcircle
    {
    public static void main(String...area)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter radius of circle");
    int r = sc.nextInt();
    float ar = 3.14F*r*r;
    System.out.println("Area of circle is = "+ar);
    }
    }

    ReplyDelete
  11. // WAP swapping two numbers
    import java.util.Scanner;
    class Swapping
    {
    public static void main(String...area)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter 1st no.");
    int f = sc.nextInt();
    System.out.println("Enter 2nd no.");
    int s = sc.nextInt();
    System.out.println("Before swapping 1st no = "+f+" and 2nd no = "+s);
    int a = f;
    f = s;
    s = a;
    System.out.println("After swapping 1st no = "+f+" and 2nd no = "+s);
    }
    }

    ReplyDelete
  12. // WAP to calculate square and cube
    import java.util.Scanner;
    class SqCb
    {
    public static void main(String...area)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a no.");
    int a = sc.nextInt();
    int s = a*a;
    System.out.println("Square of "+a+" is = "+s);
    int c = a*a*a;
    System.out.println("Cube of "+a+" is = "+c);
    }
    }

    ReplyDelete
  13. import java.util.Scanner;
    class Si
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);
    System.out.println("enter first number");
    int p = sc.nextInt();
    System.out.println("enter second number");
    int t = sc.nextInt();
    System.out.println("enter third number");
    int r = sc.nextInt();


    System.out.println("Simpleinterest=" +p*t*r/100);

    }


    }

    ReplyDelete
  14. import java.util.Scanner;
    class Tri
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);
    System.out.println("base");
    int a = sc.nextInt();
    System.out.println("height");
    int b = sc.nextInt();



    System.out.println("Area of triangle=" +a*b/2);

    }


    }

    ReplyDelete
  15. import java.util.Scanner;
    class Circle
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);

    System.out.println("r");
    int a = sc.nextInt();



    System.out.println("Area of circle=" +3.14*a*a);

    }


    }

    ReplyDelete
  16. import java.util.Scanner;
    class Sq
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);

    System.out.println("num");
    int a = sc.nextInt();

    System.out.println("sq=" +a*a );



    }


    }

    ReplyDelete
  17. import java.util.Scanner;
    class Qb
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);

    System.out.println("num");
    int a = sc.nextInt();

    System.out.println("cube=" +a*a*a );



    }


    }

    ReplyDelete
  18. Q:- WAP to calculate simple interest using Command Line Input ?
    Solution:-
    import java.util.Scanner;
    class Sis
    {
    public static void main(String args[])
    {
    Scanner si = new Scanner(System.in);
    System.out.println("Enter Principle Ammount :- ");
    int p = si.nextInt();
    System.out.println("Enter Duration in Year :- ");
    int t = si.nextInt();
    System.out.println("Enter Rate of Interest");
    int r = si.nextInt();
    System.out.println("The Simple Interest is :- ");
    System.out.println((p*t*r)/100);
    }
    }

    ReplyDelete
  19. Q:- WAP to calculate area of triangle using Command Line Input ?
    Solution:-
    import java.util.Scanner;
    class Triangle
    {
    public static void main(String args[])
    {
    Scanner at = new Scanner(System.in);
    System.out.println("Enter Breath :- ");
    int b = at.nextInt();
    System.out.println("Enter Height :- ");
    int h = at.nextInt();
    System.out.println("The Area of Triangle is :- ");
    System.out.println((b*h)/2);
    }
    }

    ReplyDelete
  20. class Cmd
    {
    public static void main(String arg[])
    {
    int p=Integer.parseInt(arg[0]);
    int r=Integer.parseInt(arg[1]);
    int t=Integer.parseInt(arg[2]);
    int si=p*r*t/100;
    System.out.println(si);
    }
    }

    ReplyDelete
  21. class Tri
    {
    public static void main(String arg[])
    {
    int b=Integer.parseInt(arg[0]);
    int h=Integer.parseInt(arg[1]);
    int a=b*h/2;
    System.out.println(a);
    }
    }

    ReplyDelete
  22. class Sr
    {
    public static void main(String arg[])
    {
    int p=Integer.parseInt(arg[0]);
    int r=Integer.parseInt(arg[1]);
    int a=p*r;
    System.out.println(a);
    }
    }

    ReplyDelete
  23. class Sw
    {
    public static void main(String arg[])
    {
    int c;
    int a=Integer.parseInt(arg[0]);
    int b=Integer.parseInt(arg[1]);
    c=b;
    b=a;
    b=c;



    System.out.println("a"+a);
    System.out.println("b"+b);
    }
    }

    ReplyDelete
  24. class simpleInt
    {

    public static void main(String args[])
    {
    int p= Integer.parseInt(args[0]);
    int r= Integer.parseInt(args[1]);
    int t= Integer.parseInt(args[2]);
    int s=p*r*t/100;
    System.out.println(s);
    }
    }

    ReplyDelete
  25. class triangle
    {
    public static void main(String args[])
    {

    int h=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);
    int a=h*b/2;
    System.out.println(a);

    }

    }

    ReplyDelete
  26. class Area
    {
    public static void main(String args[])
    {

    float r=Float.parseFloat(args[0]);

    float a=3.14f*r*r;

    System.out.println(a);

    }
    }

    ReplyDelete
  27. class squre
    {
    public static void main(String args[])
    {
    int a=Integer.parseInt(args[0]);
    int c=Integer.parseInt(args[1]);
    int x=a*a;
    int y=c*c*c;
    System.out.println("Area of="+x);
    System.out.println("cube of="+y);
    }
    }

    ReplyDelete
  28. class swap
    {
    public static void main(String args[])
    {
    int x=Integer.parseInt(args[0]);
    int y=Integer.parseInt(args[1]);
    int z;
    z=x;
    x=y;
    y=z;
    System.out.println("x="+x);
    System.out.println("y="+y);
    }
    }

    ReplyDelete
  29. Nitesh bamotriya
    calculate simple interest
    import java.util.Scanner;
    class Sii
    {
    public static void main(String[] args)
    {
    Scanner h = new Scanner(System.in);
    System.out.println("Enter principal amount");
    int p = h.nextInt();
    System.out.println("Enter rate of interest");
    float r = h.nextFloat();
    System.out.println("Enter time duration (in year)");
    float t = h.nextFloat();
    float si = (p*r*t)/100;
    System.out.println("Simple Interest is = "+si);
    }
    }

    ReplyDelete
  30. Nitesh bamotriya
    import java.util.Scanner;
    class Qb
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);

    System.out.println("num");
    int a = sc.nextInt();

    System.out.println("cube=" +a*a*a );

    ReplyDelete
  31. Nitesh bamotriya
    class Circle
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);

    System.out.println("r");
    int a = sc.nextInt();



    System.out.println("Area of circle=" +3.14*a*a);

    }

    ReplyDelete
  32. //WAP to calculate simple interest using Command Line Input?
    class SI
    {
    public static void main(String args[])
    {
    int p = Integer.parseInt(args[0]);
    int r = Integer.parseInt(args[1]);
    int t = Integer.parseInt(args[2]);
    System.out.println((p*r*t)/100);
    }
    }

    ReplyDelete
  33. class AreaOfTri

    {
    public static void main(String args[])
    {
    int b=Integer.parseInt(args[0]);
    int h=Integer.parseInt(args[1]);
    System.out.println((b*h)/2);
    }
    }

    ReplyDelete
  34. class SimpleInterest{

    public static void main(String[] args)
    {

    double p,r,t,SI;
    p=Double.parseDouble(args[0]);
    r=Double.parseDouble(args[1]);
    t=Double.parseDouble(args[2 ]);
    SI=(p*r*t)/100;
    System.out.println("Simple interet"+SI);



    }

    }

    ReplyDelete
  35. 2) WAP to calculate the area of Triangle using Command Line Input?


    class Triangle{

    public static void main(String args[])
    {

    int b=Integer.parseInt(args[0]);
    int h=Integer.parseInt(args[1]);
    int area=(b*h)/2;
    System.out.println("Area of triangle:"+area);
    }
    }

    ReplyDelete
  36. #nancy purwar


    3) WAP to calculate Area of Circle using Command Line Input?


    class Circle{

    public static void main(String args[])
    {

    int r=Integer.parseInt(args[0]);
    float area=3.14f*r*r;
    System.out.println("Area of circle:"+area);
    }
    }

    ReplyDelete
  37. #nancy purwar

    # WAP to calculate the Swap program using Command Line Input?

    class SwapCommandLine
    {

    public static void main(String[] args) {

    int a= Integer.parseInt(args[0]);
    int b= Integer.parseInt(args[1]);
    System.out.println("Before swapping value of a="+a );
    System.out.println("Before swapping value of b="+b );

    a=a+b;
    b=a-b;
    a=a-b;
    System.out.println("after swapping value of a="+a );
    System.out.println("after swapping value of b="+b );



    }
    }

    ReplyDelete
  38. #nancy purwar

    WAP to calculate Square and Cube using Command Line Input?

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

    int a=Integer.parseInt(args[0]);
    int square= a*a;
    System.out.println("square:"+square);
    int cube=a*a*a;
    System.out.println("Cube:"+cube);

    }
    }

    ReplyDelete
  39. //WAP to calculate the area of Triangle using Command Line Input?

    class Areaoftriangle
    {
    public static void main(String args[])
    {
    int b = Integer.parseInt(args[0]);
    int h = Integer.parseInt(args[1]);
    System.out.println((b*h)/2);
    }
    }

    ReplyDelete
  40. //WAP to calculate the Swap program using Command Line Input?`
    class Swap
    {
    public static void main(String args[])
    {
    int num1=Integer.parseInt(args[0]);
    int num2=Integer.parseInt(args[1]);
    int temp;
    temp=num1;
    num1=num2;
    num2=temp;

    System.out.println(num1+" "+num2);
    }
    }

    ReplyDelete
  41. -WAP to calculate simple interest using Command Line Input?

    class SI
    {
    public static void main(String args[])
    {
    int p =Integer.parseInt(args[0]);
    int t =Integer.parseInt(args[1]);
    int r =Integer.parseInt(args[2]);
    System.out.println((p*t*r)/100);
    }
    }

    ReplyDelete
  42. -WAP to calculate the Swap program using Command Line Input?`

    class Swapping
    {
    public static void main(String args[])
    {
    int a=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);
    int c;
    c=a;
    a=b;
    b=c;

    System.out.println("After Swapping "+"a= "+ a +" b= "+b);
    }
    }

    ReplyDelete
  43. -WAP to calculate the area of Triangle using Command Line Input?

    class Area
    {
    public static void main(String args[])
    {
    int base = Integer.parseInt(args[0]);
    int height = Integer.parseInt(args[1]);
    System.out.println((base*height)/2);
    }
    }

    ReplyDelete
  44. -WAP to calculate Area of Circle using Command Line Input?

    class Areaofcircle
    {
    public static void main(String args[])
    {
    int r = Integer.parseInt(args[0]);
    double area = 3.14*r*r;
    System.out.println("Area of Cicle = " +area);
    }
    }

    ReplyDelete
  45. //wap to calculate cube and square root using command line input

    class CSRoot
    {
    public static void main(String args[])
    {
    Integer n,s,c;
    n=Integer.parseInt(args[0]);
    s=n*n;
    c=n*n*n;
    System.out.println("Square of no is = "+s);
    System.out.println("Cube of no is = "+c);
    }
    }

    ReplyDelete
  46. //wap to swap two no using command line input

    class SwapTwoNo
    {
    public static void main(String args[])
    {
    int a,b,c;
    a=Integer.parseInt(args[0]);
    b=Integer.parseInt(args[1]);
    System.out.println("Before Swapping a="+a+" b="+b);
    c=a;
    a=b;
    b=c;
    System.out.println("After Swapping a="+a+" b="+b);
    }
    }

    ReplyDelete
  47. //wap to calculate area of Circle using command line input

    class AreaOfCircle
    {
    public static void main(String args[])
    {
    float ar,r;
    r=Float.parseFloat(args[0]);
    ar=r*r*3.14F;
    System.out.println("Radius is = "+r);
    System.out.println("Area of Circle is = "+ar);
    }
    }

    ReplyDelete
  48. //wap to calculate area of triangle using command line input

    class AreaOfTriangle
    {
    public static void main(String args[])
    {
    float ar,b,h;
    b=Float.parseFloat(args[0]);
    h=Float.parseFloat(args[1]);
    ar=(b*h)*0.5F;
    System.out.println("Base is = "+b);
    System.out.println("Height is = "+h);
    System.out.println("Area of Triangle is = "+ar);
    }
    }

    ReplyDelete
  49. //WAP to calculate simple interest using command line input

    class SimpleInterest {
    public static void main (String args[]) {
    float si, p, r, t;
    p=Float.parseFloat(args[0]);
    r=Float.parseFloat(args[1]);
    t=Float.parseFloat(args[2]);
    si=(p*r*t)100F;
    System.out.println("Principal is = "+p);
    System.out.println("Rate is = "+r);
    System.out.println("Time is = "+t);
    System.out.println("Simple Interest is = "+si);
    }
    }

    ReplyDelete
  50. Ankita Verma

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

    int year=1991;

    String s=(year%4==0)||(year%100!=0)||(year%400==0)?"Leap year":"not a Leap year";

    System.out.println(s);
    }
    }

    ReplyDelete

  51. Ankita Verma

    class Weak
    {
    public static void main(String args[])
    {
    char ch='7';

    switch(ch)
    {
    case'1':
    System.out.println("Today is Monday");
    break;

    case'2':
    System.out.println("Today is Tuesday");
    break;

    case'3':
    System.out.println("Today is Wednesday");
    break;

    case'4':
    System.out.println("Today is Thursday");
    break;

    case'5':
    System.out.println("Today is Friday");
    break;

    case'6':
    System.out.println("Today is Saturday");
    break;

    case'7':
    System.out.println("Today is Sunday");
    break;

    default:
    System.out.println("Invalidday");
    }
    }
    }

    ReplyDelete

  52. Ankita Verma
    import java.util.Scanner;

    class InputExample1
    {

    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the value of principle");
    int p = sc.nextInt();
    System.out.println("Enter the value of rate");
    float r= sc.nextFloat();
    System.out.println("Enter the value of time duration");
    float t = sc.nextFloat();

    float si=(p*t*r)/100;
    System.out.println("Simple interest is=" +si);

    }
    }

    ReplyDelete


  53. Ankita Verma

    import java.util.Scanner;
    class SqCube
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the value");
    int a= sc.nextInt();

    int sq =a*a;
    int c=a*a*a;

    System.out.println("squre is=" +sq);

    System.out.println("cube is=" +c);

    }
    }

    ReplyDelete



  54. Ankita Verma

    import java.util.Scanner;
    class Area
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter breadth");
    int b= sc.nextInt();
    System.out.println("Enter height");
    int h= sc.nextInt();
    int A=(b*h)/2;
    System.out.println("Area of triangle=" +A);


    }
    }

    ReplyDelete

  55. Ankita Verma

    import java.util.Scanner;
    class AreaCircle
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the value of pi");
    float p= sc.nextFloat();
    System.out.println("Enter radius");
    int r= sc.nextInt();
    float AC=p*r*r;
    System.out.println("Area of Circle=" +AC);


    }
    }

    ReplyDelete

  56. Ankita Verma
    import java.util.Scanner;
    class Swapping
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the First value");
    int a= sc.nextInt();
    System.out.println("Enter the Second value");
    int b= sc.nextInt();
    a=a+b;
    b=a-b;
    a=a-b;
    System.out.println("Swap the value a & b" + " " + a + " " + b);



    }
    }

    ReplyDelete

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