Solved

Reading an input

Posted on 2001-06-04
8
163 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now