You are on page 1of 13

Basic Programs in JAVA

Different ways to take input


Using Scanner class

BufferedReader
Scanner class
Available in util package
Syntax
Scanner sc=new Scanner(System.in);
• Create an object sc to take input from System.in
• System.in – System is a class
in is input stream
Methods of Scanner class
Data type Method use

byte byte nextByte() To take byte type value as input

short short nextShort() To take short type value as input

int int nextInt() To take integer type value as input

long long nextLong() To take long type value as input

float float nextFloat() To take float type value as input

double double nextDouble() To take double type value as input

String String next() To take String type value as input

String String nextLine() To take String type value as input

String String toString() Converts to String


Program
Import java.util.*; System.out.println(“a = ”+a);
class Input1 System.out.println(“f = ”+f);
{ System.out.println(“s = ”+s);
public static void main(String args[]) System.out.println(“c = ”+c);
{
Scanner sc=new Scanner(System.in);
int a; }
float f; }
String s;
char c;
a=sc.nextInt();
f=sc.nextFloat();
s=sc.next();
c=System.in.read();
BufferedReader
In Java, console input is accomplished by
reading from System.in.
To obtain a character-based stream that is
attached to the console, you wrap
System.in in a BufferedReader object, to
create a character stream.
BuffereredReader supports a buffered
input stream.
BufferedReader
Syntax:
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

InputStreamReader converts bytes to characters.


To obtain an InputStreamReader object that is linked to System.in,
use the following constructor:
InputStreamReader(InputStream inputStream)
Because System.in refers to an object of type InputStream, it can be
used for inputStream.
br is a character-based stream that is linked to the console through
System.in
Methods of BufferedReader
Method use
int read() throws IOException • To read a character from a
BufferedReader
• it reads a character from the input stream
and returns it as an integer value.
• It returns –1 when the end of the stream is
encountered
String readLine() throws IOException • To read a string from the keyboard
• it returns a String object

To take input for other primitive data types like int, float, double etc. parsing
methods are used.
Syntax:-
Data_type var=Wrapper_class.parseData_type(String s);

Eg:-
int a=Integer.parseInt(br.readLine());
Wrapper classes and parse methods
Primitive data type Wrapper class methods

byte Byte byte parseByte(String s)

short Short short parseShort(String s)

int Integer int parseInt(String s)

long Long long parseLong(String s)

float Float float parseFloat(String s)

double Double double parseDouble(String s)


Program
import java.io.*; System.out.println(a);
class A System.out.println(f);
{ System.out.println(c);
public static void main(String args[]) throws System.out.println(s);
IOException
{ }
catch(Exception e)
BufferedReader br=new BufferedReader(new
{
InputStreamReader(System.in));
System.out.println(“Error”);
try{ }
int a=Integer.parseInt(br.readLine()); }
float f=Float.parseFloat(br.readLine()); }
char c=(char)br.read();
String s=br.readLine();
WAP to check whether the given number is even or odd.
WAP to display sum of digits of a given number.
WAP to reverse the given number.
WAP to check whether the given number is armstrong or not.
WAP to check whether the given number is prime or not.
WAP to display following pattern
*
* * *
* * * * *
WAP to display sum of following series
1+x+x2/2!+x3/3!+x4/4!+………
WAP to calculate factorial of a number
class Factorial
{public static void main(String args[])
{
int n=5,ans;
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println(“factorial of ”+n+” = +f);
}
}
WAP to calculate factorial of a number
using function
class Factorial public static void main(String args[])
{ {
public static fact(int n) int n=5,ans;
{ ans=fact(n);
int i,f=1; System.out.println(“factorial of ”+n+” =
for(i=1;i<=n;i++) +ans);
{ }
f=f*i; }
}
return f;
}

You might also like