Enter Strings and Integers in Java
JAVA ENTERING STRING:
A string is
group of characters, alphabets, numbers enclosed within quotes.
Java
provides a special mechanism for handling exceptions (exceptions means the
wrong inputs which can cause the program to crash or fail). To handle
exceptions is to append the clause “throws IOException” to the declaration of
main() method as
Public
static void main(Strings args[]) throws IOException
Following
syntax should be followed to input string in java program:
Import java.io.*;
Public class hello
{
Public static void main(Strings
args[])throws IOException
{
InputStreamReader reader= new InputStreamReader(System.in);
BufferedReader input= new
BufferedReader(reader);
System.out.println(“Enter your name”);
String name=input.readLine();
System.out.println(“hello” +name+”!”);
}}
OUTPUT:
Enter your
name Qamar Abbas
Hello Qamar
Abbas
Explanation:
The program
prints the prompt “Enter your name” and then waits for input. When user types
“qmarabbas” and press enter, the system responses immediately and produces
output on screen “ helloQamarabbas”
The whole
scenario takes place in five steps or with five objects say system.in, reader,
input, name, and system.out. System.in and system.out objects are defined in
System Class. The other three are defined in the program on the ist, 2nd
and 3rd line.
Import Java.io.*:
it tells the java compiler to check into the java input/output library for four
classes as IOException,InputStreamReader, BufferdReader.
InputStreamReader
reader:
Reader is an instance for the class InputStreamReader. The object
reader will serve as a conduit (con-du-it), conveying data from the keyboard
into the program. It reads the bytes that come from the keyboard such as {65
108 32 71 111 114 101 13}. These are the values of byte type.
BufferedReader
input:
Input is an instance for the class BufferedReader. It extracts the
input in more convenient way. It converts the byte type input into character
type {“Qamar Abbas”}. It uses its function readLine() to read a line of
characters and assigns the string to the instance name of class String
String
name=input.readLine():
the instance gets the value from the readline()
function of called by input instance.
System.out.println(“hello”
+ name):
the system.out instance puts the stream of characters into its
println() function which displays everything in parenthesis on screen.
Java Integer Input:
import java.io.*;
public static void main(String
args[])throws IOException{
InputStreamReader reader= new
InputStreamReader(System.in);
BufferedReader input= new
BufferedReader(reader);
String text=input.readLine();
System.out.println("Enter your
age");
int age= new Integer(text).intValue();
System.out.println("you are
"+age+" years old, now");
int year=2017-age;
System.out.println("so you proablly
born in "+ year);
}
}
Everything
is same as in string input method except following lines:
String text=input.readLine():
The method readline() called by input instance reads all the input in form of
characters and assigns it to the instance of the String Class “text”.
Int age=
new Integer(text).intValue():
This expression converts the character type
input into integer type and stores it in the variable age.
Java Double Input
import java.io.*;
classAreaCircle{
public static void main(String
args[])throws Exception{
InputStreamReader reader= new
InputStreamReader(System.in);
System.out.println("Enter radius of
circle");
String text= input.readLine();
Double x=new Double(text);
double r=x.parseDouble(text);
double area=Math.PI*r*r;
System.out.println("area of circle is
"+area);
}
}
The
structure of this program is same as previous two programs except the following
lines;
Double
x=new Double(text):
x is the object of the class Double. This x has
obtained the value from text object of String class and this value cannot be
used directly in expression because value in an object is different from the
value in a variable.
Double
r=x.parseDouble():
the value of object x from Double class is assigned to a
double type integer r.
**for float
use the line (float a=new Float(text).floatValue();)