Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading an input

Posted on 2001-06-04
8
Medium Priority
?
211 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 400 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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