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 85885 // 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