Understanding Java Input and Output

Recommended by 8 users

Feb 14,2018
Understanding Java Input and Output
Add to Bookmark Email this Post 2.1K    0

Java input and output is an essential concept while working on Java programming. It consists of elements such as input, output and stream.  The input is the data that we give to the program. The output is the data what we receive from the program in the form of result.  Stream represents flow of data or the sequence of data. To give input we use the input stream and to give output we use the output stream.

How input is read from the Keyboard?

The “System.in” represents the keyboard. To read data from keyboard it should be connected to “InputStreamReader”. From the “InputStreamReader” it reads data from the keyboard and sends the data to the “BufferedReader”. From the “BufferedReader” it reads data from InputStreamReader and stores data in buffer. It has got methods so that data can be easily accessed.

Reading Input from Console

Input can be given either from file or keyword. Input can be read from console in 3 ways:

  • BufferedReader
  • StringTokenizer
  • Scanner

BufferedReader

Here, we use the class “BufferedReader” and create the object “bufferedreader”. We also take integer value and fetch string from the user.

BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
Int age = bufferedreader.read();
String name = bufferedreader.readLine();

From the eclipse window we also see an example of the same in the following code:

Bufferedreader bufferedreader = new BufferedReader(
New InputStreamReader(System.in));
System.out.println(“Enter your Name”);
String name = bufferedreader.readline();
System.out.println(“Enter your age”);
Int age = Integer.parseInt(bufferedreader.readline());
Int age1= bufferedreader.read();
System.out.println(“I am” + name + “ “+age+”years old”);
}
}

When we run the code, it will prompt the user to enter name and age.

String Tokenizer

It can be used to accept multiple inputs from console in a single line where as BufferedReader accepts only one input from a line. It uses delimiter (space, comma) to make the input into tokens.

The syntax is as follows:

BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
String input = bufferedreader.readline();
StringTokenizer tokenizer = new StringTokenizer(input, “,”);
String name = tokenizer.nextToken();
Int age=tokenizer.nextToken();

Once we enter eclipse and open the program, we view the code:

BufferedReader bufferedreader = new BufferedReader(
New InputStreamReader(System.in);
System.out.println(“Enter your name and age separated by comma”);
String Input = bufferedreader.readLine();
StringTokenizer tokenizer = new StringTokenizer(Input, “,”);
String name = Tokenizer.newToken();
Int age = Integer.parseInt(tokenizer.nextToken());
System.out.println(“I am”+name+age+”years old”);
}
}

The output will prompt user to write name and age separated by comma. In case the user enters the name and then writes the age after a space, it will show an exception error.

Scanners

It accepts multiple inputs from file or keyboard and divides into tokens. It has methods to different types of input (int, float, string, long, double, byte) where tokenizer does not have.

The syntax is denoted below :

Scanner scanner = new Scanner(System.in);
Int rollno = scanner.nextInt();
String name =  scanner.next();
In the Eclipse window we view the following code:
Scanner.scanner = new Scanner (System.in);
System.out.println(“Enter your name and age”);
String name = scanner.next();
Int age=scanner.nextInt();
System.out.println(“ I am “ “+name” “+age+” years old”);
}
}

Once we run the code, it will prompt the user to write the name and age.

Writing output to console           

The output can be written to console in 2 ways:

The First method is called print (String)

System.out.print(“hello”);

The Second method is write(int)

Int input=’i’;

System.out.write(input);
System.out.write(‘/n’);

Once we enter the Eclipse Window, we view the following code:

System.out.print(“hello”);
System.out.print(“hello”);
System.out.print(“hello”);
Int Input = ‘a’;
 System.out.write(Input);
System.out.write(“In”);
}
}

Once we run the code, we receive the output:

‘HelloHelloHello’

The ‘Println’ is a method that takes input and gives output.

I/O Streams

The I/O Stream can be divided into ‘Byte Oriented Streams’ and ‘Unicode Character Oriented Streams’. When working on ‘Byte Oriented Streams’ we can work on InputStream and OutputStream. The ‘Unicode Character Oriented Streams’ has elements such as Reader and Writer.

 Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Unveil the Magic of Java Course with Edureka!

Hadoop for Java Professionals

Learn Java/J2EE & SOA

 

Share on

Browse Categories

Comments
0 Comments
.
Ka-ching!

Find out how lucky you are. Discounts upto 100% on Edureka Courses!

Congratulations !

Congratulations !

Coupon Code Use this code on the payment page.