Unit-V Input in Java: Computer Applications-Lorven Public School, Chandapura 1
Unit-V Input in Java: Computer Applications-Lorven Public School, Chandapura 1
Unit-V Input in Java: Computer Applications-Lorven Public School, Chandapura 1
INPUT IN JAVA
Computer Applications-Lorven public school, Chandapura 1
INTRODUCTION
• What is input?
• Answer: It means providing data to the computer.
• Every language has some way to read the input. Java has the following methods to read inputs
given by user. They are,
– Using function argument
– Using InputStremReader class
– Using Scanner class
– Using command line arguments.
import java.io.*;
public class Percentage
{
int g, b, ga, ba;
float pg, pb;
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new Bufferedreader(read);
System.out.println(“Enter number of girls and boys in the class”);
g = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
ga = Integer.parseInt(in.readLine());
ba = Integer.parseInt(in.readLine());
pg = (float)(g-ga)/(g+b)*100;
pg = (float)(b-ba)/(g+b)*100;
• Values of different data types can be input using next() functions of Scanner class
Data type Functions to enter data
integer int n = sc.nextInt();
float float f = sc.nextFloat();
double double d = sc.nextDouble();
string string s = in.next() OR in.nextLine();
7
/**Program to calculate Employee’s Gross & Net Salary by using Scanner Class */
import java.util.*;
public class Employee_Salary
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter employee’s name and basic salary”);
double basic, da, hra, pf, gp=0, np=0;
String empn;
empn = in.nextLine();
basic = in.nextInt();
da = basic*25.0/100.0;
hra = basic*15.0/100.0;
pf = basic*8.33/100.0;
gp = basic+da+hra;
np = gp-pf;
System.out.println(“Name of the employee = ” + empn);
System.out.println(“Gross Pay = Rs.” + gp);
System.out.println(“Net Pay = Rs.” + np);
}
}
• While accepting the values from the console, the system stores the data values in different
locations as an array of strings.
• The arguments to the main function are passed through args[0], args[1], agrs[2], and so on.
• To input the values using command line arguments use a string type array as an argument to the
main function as below:
public static void main(String args[])
Step 5: Enter the value as ‘strings’ in the space provided under the
‘Expression.main()’ in the method call window.
Step 6: Click Ok.
Computer Applications-Lorven public school, Chandapura 14
The output of the program will appear on the terminal window.
• Syntax error
• Logical error
• Run time error