Do you use System.in.read()??

As above, don't u guys find that java reading in of inputs from the command line are pretty cumbersome?

This is good I use it.
* Read a line of input from System.in and convert the entire line to
* a String or a number (int or a double). Kills the program on user
* error.
* @author Suzanne Menzel
* Usage: Keyboard.readLine() ==> next line of user input as a String
* Keyboard.readInt() ==> next line of user input as an int
* Keyboard.readDouble() ==> next line of user input as a double
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
public class Keyboard { 
public static String readLine() {
String line = "";
try {
line = reader.readLine();
catch (IOException e) {
System.out.println("Error in Keyboard class:" + e);
quit();
return line;
public static int readInt() {
int n = 0;
try {
n = Integer.parseInt(readLine().trim());
catch (NumberFormatException e) {
System.out.println("Error in Keyboard.readInt()\n" + e);
quit();
return n;
public static double readDouble() {
double n = 0.0;
try {
n = Double.parseDouble(readLine().trim());
catch (NumberFormatException e) {
System.out.println("Error in Keyboard.readDouble()\n" + e);
quit();
return n;
private static void quit() {
System.out.println("\nTerminating the program.");
System.exit(1);
private static BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));

Similar Messages

  • I was using system.in.read function...

    i was using system.in.read function to read input.when i used the his function
    again for the second time it read the same input which was read first
    which is pretty obvious that the initial contents of the input buffer still
    contains the previos contents which is not being overwritten by the new input
    contents.can someone please tell me how to read the contents using the
    same function.i know there are a lot of other ways to accepting inputs
    but i wanted to clear this doubt of system.in.read before moving on to
    ip/op chapter of java..........

    if your reading textual data use a buffered reader.
    BufferedReader br = new BufferedReader(
                            new InputStreamReader(System.in));It will make your life easier.
    The System.in doesnt appear to support a flush method by itself. Also it typically reads in byte data, so you would have to build your strings to manipulate them.
    Unless of course, this is what you want.

  • What are you using as RSS Reader on iPhone?

    I am an RSS junkie, and this is how I quickly get my news every day. If you have not yet discovered the power of RSS, it lets you quickly browse headlines from dozens of web sites in one-stop shopping.
    What have you found to be the best way to manage and read RSS feeds on your iPhone?
    I had been using a Mac-based app called NewsFire on my MacBook, but they told me there is 0% chance of porting this to be an iPhone app.
    So I am shopping for a new RSS reader to manage all of the feeds I subscribe to.
    What RSS reader do you like on the iPhone? Are you just using Google Reader or Safari, or have you found something cooler? What do you like about the reader you chose?
    Thanks!
    Brian

    I haven't used RSS on iPhone, but I understand if you click an RSS feed in iPhone Safari it'll use
    reader.mac.com, though apparently it doesn't have many features. Maybe more sites or widgets will come out sometime?
    You can ask Apple for iPhone changes via this link:
    http://www.apple.com/feedback/iphone.html Though if you're requesting hardware changes for iPhone v2 you may want to wait a week (or a month?)...they're probably going to be inundated right now by requests for software changes to the current iPhone.

  • How do you use adobe acrobat reader that can be used/view as a PDF in a web browser

    I get a message that say: this abode acrobat reader that is running can not be used to view PDF files in a
    Web Browser.

    Hello there,
    This forum is for questions about Acrobat.com, and we cannot offer assistance for questions about other Adobe products and services. If you are having trouble with Adobe Reader, I recommend looking at the Reader forum:
    http://forums.adobe.com/community/adobe_reader_forums/adobe_reader
    Here is a search that I did for the error message you reported; it looks like there may be some helpful threads already posted there:
    http://forums.adobe.com/search.jspa?q=view+PDF+files+in+a+web+browser&resultTypes=MESSAGE& dateRange=all&communityID=3414&username=&numResults=15&rankBy=10001
    Best of luck!
    Kind regards,
    Rebecca

  • Bufferreader & system.in.read

    hello, everyone
    I am confused by the java input from the keyboard. Why sometime we use bufferreader, but sometime using system.in.read? What is their difference? When should I use bufferreader and system.in.read?
    Thank you!~

    System.in is used when you want to read from the keyboard.
    BufferedReader is a class that you can wrap around any other reader, and it provides buffering so that performance is better. A BufferedReader isn't tied to a particular physical device.

  • How to "kill" AWT Event Queue thread without using System.exit()?

    When I run my program and the first GUI window is displayed a new thread is created - "AWT-Event Queue". When my program finishes, this thread stays alive and I think it causes some problems I have experienced lately.
    Is there any possibility to "kill" this thread without using System.exit() (I can't use it for some specific reasons)

    All threads are kept alive by the JVM session. When you use System.exit(int) you kill the current session, thus killing all threads. I'm not sure, though...
    What you could do, to make sure all threads die, is to make ever thread you start member of a thread group. When you want to exit you app, you kill all the threads in the thread group before exit.
    A small example:
    //Should be declared somewhere
    static ThreadGroup threadGroup = new ThreadGroup("My ThreadGroup");
    class MyClass extends Thread {
    super(threadGroup, "MyThread");
    Thread thread = new Thread(threadGroup, "MySecondThread");
    void exit() {
    //Deprecated, seek alternative
    threadGroup.stop();
    System.exit(0);
    }

  • Getting a number from  (char)System.in.read

    I am very new to java so please forgive the seemingly basic question.
    When I try to simply get a number for input into a program, and I use (char)Sysytem.in.read(),
    it only gives me the acutual Keyboard number (ie.. 2 = 50, 1 = 49).
    Even when I try to parse an Integer using :
    public static void getNum( ) throws NumberFormatException
         char char1;
         String String1 = new String ("");
         int X;
         char1 = (char)System.in.read();
    while ( char1 <= '0' && char1 <= '9' )      
         String1 = String1 + char1;
         char1 = (char)System.in.read();
         X = Integer.parseInt(String1);
    System.in.read();
         return X;
         System.out.println(X);
    This gives me a usable number but if I try to use a decimal point in the number and convert it to a double, it wont work because the decimal wont read into the (char)System.in.read();
    Anyone have a suggestion ?

    Hope this helps - although haven't tried out your code.
    System.in.read() returns the next byte of information in the buffer, if the buffer is empty it'll prompt for input (passing control back to hardware/OS). Say the user types 10.56 and presses return - now the buffer has the information 10.56 plus a LF(line-feed) and and a CR(carriage return) character (this is how it is from DOS anyway) and control is passed back to your program which gets only the first byte of information from the buffer, which in this case would represent the character '1', only it doesnt get back the character '1', it gets an int type which represents the byte of information which represented the character '1' to your hardware/OS system (which I believe is decided by the machine local encoding system).
    By casting this int to a char by the expression
    (char) System.in.read()
    its converted to the original character that the int represented from the buffer
    If you were using System.in.read() only you would have to reconstruct the input as a String by concatenating the char's one by one only ignoring the last 2 bytes of the buffer (which captured the LF and CR).
    Say you had the resulting String from the above reconstruction in the variable s1,
    you could then attempt to convert the resulting String to a floating-point type (say a double) using
    d = Double.parseDouble(s1)
    where d was a double type (as mentioned by abnormal)
    You'd have to put this in a try/catch statement because the user could type something invalid like 10.6a5
    which would cause the parseDouble() method to throw an exception which unless you handle it yourself will cause your program to abend with a NumberFormatException.
    This all seems to be a lot of hard work for input apparently so simple, which is why abnormal suggest using a BufferedReader to overcome the problem of 'manually' stripping the CR and LF from the input leaving you with just a String. I must admit I'm not sure of why abnormal's code wouldn't work for you. But I thought I'd post this anyway because you might find the background info helpful.

  • What is facetime? And how do you use it?

    What is facetime and how do you use it?

    rblue80 wrote:
    What is facetime and how do you use it?
    What is Google and how do you use it?

  • Is it possible not to use System.out.println?

    Hi guyz,
    Can anyone tell me if there is a way not to use System.out.println but produce the output on the command prompt? I need to add two numbers which are accepted through the args(command prompt). I have to add them and show them on command prompt without using System.out.println. Can ANYONE HELP?

    Hi guyz,
    Can anyone tell me if there is a way not
    s a way not to use System.out.println but produce the
    output on the command prompt? I need to add two
    numbers which are accepted through the args(command
    prompt). I have to add them and show them on command
    prompt without using System.out.println. Can ANYONE
    HELP?Why can't you use System.out.println? What's the reason?
    Kaj

  • Discussion: What file system do you use for external backups and why?

    I just got a brand new seagate 2TB hdd to act as an external backup.
    I use ext2 with it, because it's USB (sata->usb enclosure), and I'm not likely to connect it direct to a PC anytime soon (also my computer is old and I liek to keep ram usage down as much as possible)...
    What filesystems do you use\prefer for external backups.. and why?
    Also, what type of connection do you use (USB,eSATA,etc,) and size (ie larger then a disk on key..)?

    I used to use ext4 on a 1.5TB HD, but recently switched back to ext3—and a tiny NTFS partition for the ext2fsd install file. I realized I use this drive a lot more outside of my Linux environment than I thought, and it was a hassle (and also unsafe) to disable driver signatures at Windows boot just to get a dev version of ext2fsd to work.
    pseudonomous wrote:If portability to other operating systems is a concern, then fat32 is, unfortunately, still the best choice, unless you have single files bigger than 4gb, which would probably mean you want to use ntfs or ext2.
    While helping a friend with her external hard drive, I noticed another limitation of fat32: only 65535 file descriptors per directory. She tried to backup her entire NTFS Windows C: drive to a fat32 one. Took us a good while until we figured that out, and I couldn't find much about it on the net.

  • Can you use final cut pro and logic, after effect with lion operating system or not

    This question to apple people as well, can you use all previous softwares on lion or not?

    Susie Summer wrote:
    PS I had no problem exporting previous projects - proof that I had Compressor!!!!!!
    Actually, no. Compressor is a separate application is NOT required in order to export from FCP X. Lots of people buy FCP X but not Compressor.
    Something in your system got hosed.
    I suggest you delete FCP X completely from your system using FCS Remover, and then download it from your Purchases in the App Store.

  • Error message Indesign:Either the file does not exist, you do not have read access to it, or use the file in another application.

    I've got problem with opening a file in InDesign. The file type is an InDesign Markup Document. The error message is: Either the file does not exist, you do not have read access to it, or use the file in another application. What's wrong?

  • I keep getting an error message that reads "to update application sign into the account you used to purchase it"   I am trying to download Final Cut Pro X 10.0.9  I am signed in with the account that I purchased Final cut Pro X   I don't have another acco

    I keep getting an error message that reads "to update application sign into the account you used to purchase it"
    I am trying to download Final Cut Pro X 10.0.9
    I am signed in with the account that I purchased Final cut Pro X
    I don't have another account I'm not sure what's going on

    Welcome to Apple Support Communities
    Open the Mac App Store and go to Purchases. Do you see Final Cut Pro X listed?
    If so, open a Finder window, choose Applications in the sidebar and delete Final Cut Pro X. Then, go to the Mac App Store and download Final Cut Pro X for free.
    If you don't see it in Purchases, see if it's hidden, and if so, unhide it > http://support.apple.com/kb/HT4928 If it doesn't work, contact with the Mac App Store support > http://www.apple.com/support/mac/app-store/contact

  • Presently using iMac: OS X 10.9.4 (13E28)  1. Open website; click drop-down and this appears:  Before viewing PDF documents in this browser you must launch Adobe reader and accept the end-user license agreement, then quit and relaunch the browser.  2. Usi

    Presently using iMac: OS X 10.9.4 (13E28)
    1. Open website; click drop-down and this appears:
    Before viewing PDF documents in this browser you must launch Adobe reader and accept the end-user license agreement, then quit and relaunch the browser.
    2. Using an iMac, open founder and click downloads
    3. Click AdbeRdr1013_en_US-1.dmg         Apr. 4 2012, 7:11 AM 73 MB Disk Image  Feb 27, 2013, 10:00 AM
    4. Screen indicates it is opening Adobe reader X installer.package
    5. Click this icon
    6. Open Adobe installer; double-click install Adobe reader; putting password to access; click next; Note: Application is already installed; click finish.
    Screen indicates: thank you and enjoy Adobe reader. Click Return to Prior Website. Click install now: total size 99.1 MB
    Initializing arises with the following information: to proceed, open your download folder and locate the Adobe reader installer file, named Adobe reader installer [ XXX].dmg
    It takes me back to steps that have already taken of locating down load file double-click to mount the disk image; double-click install Adobe reader; agreed by clicking open; entering name and password and clicking okay.
    Did this several times with no success.
      11.       www.forums.adobe.com

    Sorry if I failed to mention, I have done this as well.

  • What version control system you use?

    We are 20 developer and we use bitkeeper as a version control system.
    But we have 4 envirronnement.
    dev/test/pre-prod/prod.
    Sometimes, 2-3 projects are affecting the same packages.
    And different packages version is all over the place since we have 4 envirronnements.
    What version control system you use for packages?
    Would be cool to have a version control system intergrated to our tools, like toad, pl/sql dev or sql developer, is there one for those tools?
    Is the tool you using it doing all you want it to do?
    Have any link for me to check out?

    Hi,
    I recommend SourceAnywhere Standalone to you. It is an SQL-based source control application that provides all of the key features of VSS, plus much more. It is well integrated with Microsoft Visual Studio 6/2003/2005/2008, Dreamweaver and Eclipse. Here is the home page of SourceAnywhere Standalone:
    http://www.dynamsoft.com/Products/SAWstandalone_Overview.aspx
    The Hosted edition, SourceAnywhere Hosted that is delivered as a SaaS application is also available.
    http://www.dynamsoft.com/Products/SAWhosted_Overview.aspx
    You can take a look.
    Thanks,
    Catherine Sea
    www.dynamsoft.com
    the leading developer of version control and issue tracking software
    Message was edited by:
    Catherine Sea

Maybe you are looking for