Solved

Reading an input

Posted on 2001-06-04
8
190 Views
Last Modified: 2010-03-31
How do I input a data to a Java program through keyboard?
0
Comment
Question by:NgSheauLan
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Accepted Solution

by:
chrisos earned 100 total points
ID: 6152072
Here is a quick example of getting the user input from the keyboard.

Regards,

Chrisos.

//////////////////////////////////////////////

package com.wintermuteis;

import java.io.*;

public class InputExample extends Object {

     public static void main(String[] args) {
          InputStreamReader reader = new InputStreamReader(System.in);
          BufferedReader in = new BufferedReader(reader);
          String line = null;
          int counter = 0;
          try {
               while( line == null || !line.equals("quit") ) {
                    while( !in.ready() ) {
                         try {
                              Thread.sleep(1000);
                              System.out.println("Sleeping..."+(++counter));
                         }
                         catch( InterruptedException ie ) {
                              // Ignore
                         }
                    }
                    line = in.readLine();
                    System.out.println("Input: "+line);
               }
          }
          catch( IOException ioe ) {
               ioe.printStackTrace();
          }

     }
}
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6155077
Hi Nq...


U can use System.in or send it as a argument to the class.


Cheers
Shyam
0
 

Author Comment

by:NgSheauLan
ID: 6156227
Hai Chrisos,
Is there any keyword similar to "cin" of C++ in Java?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:chrisos
ID: 6156250
Nq,

Sorry, I don't no much about C++, as far as I do understand C++ I don't think there is a stream predefined except as System.in

Chrisos.
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6158502
Hi Nq.

U don't have something similiar to cin in java other than System.in

That is the only way or u have enter as argument to your application are the only method,


Shyam

0
 

Expert Comment

by:surendraboppana
ID: 6160266
here is a sample program to read input from the keyboard


import java.io.*;


class reading throws IOException
{
   public void main(String args[])
   {
     /*defining a buffered reader which is ready to   read   input from the keyboard
     */  
       BufferedReader br=new BufferedReader(new     InputStreamReader(System.in));

       String temp;
     
      do
      {
            temp=br.readLine();

            System.out.println("u  entered"+temp);
          /*reads line by line from the keyboard  until
             u hit return key and stores it in temp in
             string form.parse it into int/float if
             necessary
          */
       }while(!temp.equals("stop"));//reads until some
       //specified string(here "stop") is encountered
     }
}


i myself have used this snippet in my program for reading input from keyboard.this is sure to work


0
 
LVL 3

Expert Comment

by:chrisos
ID: 6160712
surendraboppana,

Please read the following:

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

It is considered bad form to propose an answer that is identical to someone else's comment.

Nq,

You are at liberty to reject propsed answers and accept comments as answers should you wish to do so.

Regards,

Chrisos.
0
 

Author Comment

by:NgSheauLan
ID: 6170477
surendraboppana,

Thank you for your help. I am sorry that I have to reject your answer because I have clicked the icon accepted comment as answer one day before your reply.

Any way, thanks again for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 52
difference between sorce folder and folder in eclipise 3 40
servlet and mdb, jms error 1 54
DTD and JAVA versions 1 27
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question