Skip to main content

User Input Mechanism in Java

Recently while writing some java program. I wanted to quickly run some test cases using runtime inputs from console. I am working on some of the legacy system which runs on java1.5 and recently has been upgraded to java1.6 version.
For taking user input during run time, I have always used Scanner API. But recently came to know about Console class. Though it was included as part of Java1.6 release. But I never used it any time. Just wanted to share some information on the available API which is used for user input:

-Scanner (Included in Java1.5)
-BufferedReader (Included in Java1.1)
-Console (Included in Java1.6)

From developers point, I assume following factors could be considered during the selection of any of the above APIs:
If your input size is more, or more number of inputs is going to be used in the program, it is advisable to use BufferedReader instead of Scanner. It will be difficult to observe the speed for fewer inputs. But for huge data (for millions of inputs), definitely there will be an edge in using BufferedReader. Scanner could read anything, for example lines, whitespace as tokens, any other regular expression tokens, characters or numbers. While there is limitation in reading regex for BufferedReader and Console. They could read characters, arrays etc. BufferedReader and Console does not facilitate any kind of parsing unless it is handled explicitly. The Exception handling works differently for each of these. Scanner and Console do not throw Checked exception where as BufferedReader complains for Checked Exception. Scanner isn’t synchronized, BufferedReader synchronized.

These are the few points that could be helpful in understanding the mechanism of taking input from user in Java.


Feel free to add any more points. 

Comments

Popular posts from this blog

Know your Repository Statistics

Being in software development, everyone of us must be using some or the other repository to save our work( popularly know as check-in check-out :) ). Recently while working on one of my project I thought of finding out the statistics of our project repository for some management reports. While there are so many tools available in the market to explore the stats, I chose to go with Tortoise SVN tool with some plugins. Following are other tools that can be very useful based on scenarios: -Commit Monitor -Winmerge -Visual SVN -SVN Monitor -CM Synergy from Telelogic -Many more are there If you are using Tortoise SVN and want to know the details(for example : no of java classes checked-in, lines of codes written, developers name, total code base details and many more ) about your repository You can use the following steps to find the details: 1-check if the SVN has been installed and working properly or not by using following command: C:\>svn help It will output something ...

Testing your Webservice Applications using SOAP UI

SOAP UI is a standard desktop application for testing the Web Services projects. It provides full support for debugging, developing and testing of your web services applications. The tool support data driven development and also provides platform for creating test suites where you can create services for regression testing. For example if you want to test the complete flow of your SOA application, you can create Test Suites using SOAP UI and can perform end to end testing of your applications. The test suits can be configured to run in multiple environments (dev, sit, uat or production). Okay, let’s start the working on SOAPUI. I will show you the simple webservice testing that I developed in my last blog. Prerequisites: -You have developed your webservices -Webservice is ready and running on your local server -Installed SOAP UI tool Step-1 Download the SOAPUI tool Step-2 Open the soap UI Tool Setp-3 Right click on the project and choose New SOAP Pro...

It's All Mobile

In the digital era, where anything is available on finger tip and we want everything in our pocket, It will not be wrong to say that mobile devices have become our life line. These devices are impacting our lives on a daily basis. Today we have an app(as the modern world calls it) for everything. You name an application, its there on app store/play store. For any company(small or big), having a mobile application can be a smart way to connect with people and get started with their business very quickly.  I can say it has become a must first step for businesses to have mobile presence if they want to reach out to their customers easily and on time. This is high time for them to get their feet wet and jump into this App world. Today's world of technical innovation is taking business owners closer to their customers and giving them opportunity to know the customers need. This can be a good way to understand your customer instead of solving their problem. Demand for mobile software a...