Link to home
Start Free TrialLog in
Avatar of NgSheauLan
NgSheauLan

asked on

Reading an input

How do I input a data to a Java program through keyboard?
ASKER CERTIFIED SOLUTION
Avatar of chrisos
chrisos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Nq...


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


Cheers
Shyam
Avatar of NgSheauLan
NgSheauLan

ASKER

Hai Chrisos,
Is there any keyword similar to "cin" of C++ in Java?
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.
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

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


surendraboppana,

Please read the following:

https://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.
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.