Java/J2EE and SOA (348 Blogs) Become a Certified Professional

Understanding Java Input and Output

Last updated on Jun 17,2021 26.7K Views


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. In java, input can be read from console in 3 ways:

  • BufferedReader
  • StringTokenizer
  • Scanner

BufferedReader – Java class

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 name”);
String name = bufferedreader.readline();
System.out.println(“enter 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 in java ide, it will prompt the user to enter name and age as you can see below,

java output in eclipse

String Tokenizer – Java class

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.

Scanner – Java class

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.

Types of I/O Streams in Java

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.

Thus we have come to an end of this article. If you wish to learn more, check out the Java Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

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

edureka-logoRelated Posts:

Unveil the Magic of Java Course with Edureka!

Hadoop for Java Professionals

Learn Java/J2EE & SOA

 
Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Understanding Java Input and Output

edureka.co