An Exclusive Data Input methods in JAVA
Hard Coded Values:- The values are stored in a variable and used later.
Data input methods in java: There are many data input methods in java like Hard coded values, through main method, with Scanner class, with Buffered Reader
click here for java videos http://www.youtube.com/trushnatej
Hard Coded Values:-
The values are stored in a variable and used later.
public class AddTwoNumbers
{
public static void main()
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of numbers: "+sum);
}
}This method is easy and simple but it gives same output every time
Taking values through main method
Pubic static void main(int a, int b)
{
int sum=a+b;
System.out.println(“The sum is”+sum);
}This method is easy, simple, different values can be passed for each execution according the input. It will give different output
Scanner class:-
The scanner class is in util package so this statement is compulsory if you want to take input through scanner class.
import java.util.*; Make an object of the class Scanner.
Scanner sc=new Scanner(System.in); It is used to take input through the keyboard using different methods according the data type.
The Scanner class methods are as follows.
<tbody>
boolean nextBoolean()
To read a boolean value from the user
byte nextByte()
To read a byte value from the user
double nextDouble()
To read a double value from the user
float nextFloat()
To read a float value from the user
int nextInt()
To read an int value from the user
String nextLine()
To read a String value from the user
long nextLong()
To read a long value from the user
short nextShort()
To read a short value from the user
</tbody>
Advantages
Data is input
at run time.
Gives output
according the input
import java.util.Scanner;
public class AddTwoNumbers2
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: "); num2 = sc.nextInt();
sum = num1 + num2;
System.out.println("Sum of these numbers: " + sum);
}
}Output:
Enter First Number: 121
Enter Second Number: 19
Buffered Reader :
Buffered Reader class is used to take input in java. It has only one method to take input from the user at run time. It is in java.io package. The object of the class is created with this syntax.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
The input is taken in string form only.
Write a program to add two numbers (taking input with BufferedReader)
import java.io.*;
class Addition {
public static void main() throws Exception
{
int a, b, c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the two numbers to add:");
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
c = a + b;
System.out.println("\n Sum of two numbers:" + c);
}
}Output:
Enter the two numbers to add :2.3 3.5
Sum of two numbers: 5.8
For more java tutorials click
Data types in JAVA
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.