Skip to main content

Chapter 7 What is Constant and How to get User Input in java Explain in Details

 Constant and User Input 

What is Constant in Java ?

  • An element of program whose value can not be changed ( Static type) at the time of execution of program is called constant.
  • It is also called literals.
  • It may be int,float and character data type.

Note:- Guys Next Chapter discuss on Data Types Topics in Easy Way- Chapter 8

Rules for Constructing Integer Constant 

  • It must have atleast one digit.
  • It must not have a decimal point.
  • It may be positive or negative.
  • No comma or blank space are allowed in integer constant.

Rules for Constructing Floating Point Constant 

  • It must have atleast one digit.
  • It must have a decimal point.
  • It may be positive or negative.
  • No comma or blank space are allowed in floating point constant.

Rules for constructing Character constant

  • It is a single alphabet,digit or special symbol.
  • The length of character constant is 1 character.
  • Character constant is enclosed within single quotes (Example char c='A';).

User input in Java 

1. Import Scanner in java:

  • 1.Scanner is a predefined class which is used to take user input in java.
  • 2.Scanner class is defined inside java.util package.
  • 3.Scanner class consists of many function to take user input which is given below.

Some Important Point: 

  • nextInt():Used to read integer value from the user.
  • nextFloat():Used to read float value from the user.
  • nextDouble():Used to read double value from the user.
  • next():Used to read string value without space from the user.
  • nextLine():Used to read string value from the user.
  • nextByte():Used to read byte value from the user.
  • nextShort():Used to read short value from the user.
  • nextLong():Used to read long value from the user.

Example 1 : User Input in Java

  • import java.util.Scanner; // Import the pakage for java


//Q1 Print Student Details
import java.util.Scanner;
class Stud{  
  public static void main(String[] args) {  
    String name;
    int rollno;
    float marks;
    //instance of class Scanner
    Scanner obj= new Scanner(System.in);  
// // Get the value by user
    System.out.println("Enter your name");
    name = obj.nextLine();  //taking string input

    System.out.println("Enter your rollno");
    rollno = obj.nextInt();//taking integer input

    System.out.println("Enter your marks");
    marks = obj.nextFloat();//taking float input

//printing the output
    System.out.println("Name=" + name);
    System.out.println("Rollno=" + rollno);
    System.out.println("Marks=" + marks);
  }
}


### OUETPUT###

   
    Enter your name
    Annu Priya
    Enter your rollno
    82006
    Enter your marks
    475
    Your Name=Annu Priya
    Your Roll Number=82006
    Your Marks=475.0


Q2. Addition two Number :  


import java.util.Scanner; //import the java pakage
class Addd{  
  public static void main(String[] args) {  
// Add of Two Number
Scanner sc= new Scanner(System.in);  

        System.out.println("Enter 1st Number:- ");
        int a= sc.nextInt();
        System.out.println("Enter 2nd Number:- ");
        int b= sc.nextInt();
        int c= a+b;
        System.out.println("Total Value: " +c);

  }
}


### OUETPUT###

   
    Enter 1st Number:-
    45
    Enter 2nd Number:-
    65
    Total Value: 110



2.Import ReaderStream in java

  • 1.BufferedReader is a predefined class which is used to take user input in java.
  • 2.It is defined inside the java.io package.
  • //importing the package

  • import java.io. *; // use only 
  • or 
  • import java.io.BufferedReader;
  • import java.io.InputStreamReader;


//importing the package
import java.io.*; // you can use only for
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Easy
{
 public static void main(String[] args)
 {
  //creating the instance of class BufferedReader
 BufferedReader reader=new BufferedReader
(newInputStreamReader(System.in));
  String name;
   try {
  System.out.println("Enter your name");
  name=reader.readLine();//taking string input
  System.out.println("Name="+name);
    } catch (Exception e) {
    }
 }
} //I think this very difficult face so ignore this step



### Output ###


Enter your name :Shailesh
Name: Shailesh


3.Use Command Prompt in java:

If you want using command prompt get value by user then use cmd application :
Let's See Some Example for this: 

Example 3.1: Concatenation of value 

What is  concatenation in Java?
Concatenation in the Java programming language is the operation of joining two strings together. You can join strings using either the addition (+) operator or the String's concat() method.


class Cmd{
    public static void main(String as[]){

        System.out.print(as[0]);
        System.out.print(as[1]);

        System.out.println(as[1]+ as[0]);
    }
}

### Output ###


PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\javaclass> java Cmd
5Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
        at Cmd.main(8thDInp.java:12)


  • How to solve Index 1 out of bounds.............. problem:
  • Note : Which time run program using java-file name  '' java Cmd''
  • But Note Show your current value then time used 
  • java-file name and value according to your arrays index
  • for example :-    

    java Cmd 5 8      
    5885 // Current Output

  • So follow this step solve your index bounds problem.
  • Your current value show concatenation type. 

Guys if you want solve Concatenation problem, how to get my exact value then using Wrapper class :

Example 3.2: Using Wrapper class solve concat() problem.


 
// Program : Addition of Two value
class Cmd1{
    public static void main(String ar[]){
        System.out.print( Integer. parseInt(ar[0]) + Integer. parseInt(ar[1]));   //find one line

    }
}


### Output ###


java Cmd1 5 8    
13

Example 3.3 : Using Wrapper but JavaScript Type



class Cmd2{
    public static void main(String ar[]){
        int a= Integer. parseInt(ar[0]);
        int b= Integer. parseInt(ar[1]);
        int c= a+b; // Find JavaScript Methods
        System.out.println("Addition = "+c);
    }
}



### Output ###


 java Cmd2 4 9    
Addition = 13






I Hope your doubt has been clear in this page so Don't  forget follow me.

Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...