Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit-V Input in Java: Computer Applications-Lorven Public School, Chandapura 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

UNIT-V

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.

Computer Applications-Lorven public school, Chandapura 2


USING FUNCTION ARGUMENT
• In this input method, the variables whose class Table
{
values are to be input must be provided as
public static void main(int n)
arguments to the main function. {
– Example: public static void int i, sum;
main(int a, int b) for(i=1; i<=10; i++)
{
sum=i*n;
System.out.println(sum);
}
}
}

Computer Applications-Lorven public school, Chandapura 3


public class Difference
{
public static void main(int p, int r, int t)
{
double si, ci=0, amt, diff=0;
si = p*t*r/100.0;
amt = p*(Math.pow(1+r/100.0, t));
ci = amt-p;
diff = ci-si;
system.out.println(“The compound interest = Rs. ” +
(float)ci);
system.out.println(“The simple interest = Rs. ” + si);
system.out.println(“The difference between CI and SI = Rs. ”
+ (float)diff);
}
}

Computer Applications-Lorven public school, Chandapura 4


USING STREAM CLASS

• java.io.* package contains InputStreamReader //integer


int n = Integer.parseInt(in.readLine());
class. Import this package.
import java.io.* //float
float n = Float.parseFloat(in.readLine());

• Create objects: //double


double n = Double.parseDouble(in.readLine());
InputStreamReader read = new
InputStreamReader(System.in); //char
BufferedReader in = new char ch = (char)(in.read());
BufferedReader(read);
//string
string str = in.readLine();

Computer Applications-Lorven public school, Chandapura 5


//To find percentage of girls and boys in the class

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());

System.out.println(“Enter number of girls and boys absent in the class”);

ga = Integer.parseInt(in.readLine());
ba = Integer.parseInt(in.readLine());

pg = (float)(g-ga)/(g+b)*100;
pg = (float)(b-ba)/(g+b)*100;

System.out.println(“Percentage of girls present in the class = ” + pg);


System.out.println(“Percentage of boys present in the class = ” + pb);
}

Computer Applications-Lorven public school, Chandapura 6


USER SCANNER CLASS
• Scanner class is a member of java.util package. Import this package.
import java.io.*; OR import java.util.Scanner;

• Create object of Scanner class in the main function:


Scanner sc = new Scanner(System.in);

• 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);
}
}

Computer Applications-Lorven public school, Chandapura 8


USING COMMAND LINE ARGUMENT

• 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[])

public static void main(String []args )

Computer Applications-Lorven public school, Chandapura 9


• The values of different data types can be input as per the table shown below:

Types of data to be entered Functions to input data


Integer int n = Integer.parseInt(args[0]);

Float float n = Float.parseFloat(args[0]);

Double double n = Double.parseDouble(args[0]);

string string str = args[0];

Computer Applications-Lorven public school, Chandapura 10


Program to find the value of the expression

/**Program to find the value of the given expression


*/

public class Expression {


public static void main(string Args[]) {
int a, b;
double p;
a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[1]);
p=(double)(a*a + b*b)/(a-b);
System.out.println(“The value of the
expression = ” + p);
}
}

Computer Applications-Lorven public school, Chandapura 11


COMPILATION & EXECUTION OF THE
PROGRAM
Step1: Compile & close the program

Computer Applications-Lorven public school, Chandapura 12


Step 2: Select the folder ‘Expression’
Step 3: Select void main(String [ ] args) from dropdown list

Computer Applications-Lorven public school, Chandapura 13


Step 4: A method call window will appear on the screen.

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.

Computer Applications-Lorven public school, Chandapura 15


TYPES OF ERRORS

• Syntax error
• Logical error
• Run time error

Computer Applications-Lorven public school, Chandapura 16


COMMENTS IN JAVA

• Single line comment (//)


• Multi line comment (/* ………………… */)
• Documentation comment (/** ………………… */)

Computer Applications-Lorven public school, Chandapura 17

You might also like