Solved

Reading an input

Posted on 2001-06-04
8
182 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 90
xampp tool 12 50
jdbc error in jsp application 20 45
difference between sorce folder and folder in eclipise 3 13
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 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