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?



                                            


                         






19 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

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