Solved

Reading an input

Posted on 2001-06-04
8
173 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

932 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

11 Experts available now in Live!

Get 1:1 Help Now