Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java equivalent of C++ "cin >>" ?

Status
Not open for further replies.

stevepuri

Programmer
Mar 30, 2001
24
0
0
US
Hey,

Does anyone know how to do
the Java equivalent of the
C++ "cin >>" storing into a
variable operation?

C++
Code:
int x = 0;
cout << &quot;Enter a number: &quot;;
cin >> x;

Java
Code:
int x = 0;
System.out.println(&quot;Enter a number: &quot;);
x = System.in.readLine(); // This don't work!

Thanks,
Steve.
 
You have to turn the input string into a number (the overloaded operators do this automatically with cin)

int x = 0;
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(&quot;Enter a number: &quot;);
String input;
try {
input = in.readLine();
x = Integer.parseInt(input);
} catch (NumberFormatException nfe) {
System.out.println(&quot;Invalid number: &quot; + input);
}
 
Given that this is quite a common process required for non-graphical user interfaces, you might want to create a class specifically for handling keyboard input.

To use the class, you would do something like this...

Code:
KeyboardInput keyIn = new KeyboardInput();
keyIn.prompt(&quot;Type in a number&quot;);
int x = keyIn.getInt();

And the full class is below...

Jo.


Code:
import java.io.*;		// For data input streams.

//  **************************************************************
/** Class to handle keyboard input. Prompts for user's keyboard 
  * input and converts input string into numbers. Picks up 
  * conversion errors.
  * @author   Jo Wood.
  * @version  1.4, 30th May, 2001
  */
//  **************************************************************

public class KeyboardInput
{   
    // ----------------- Object variables ------------------
    
    private String textLine;  // Line of text input from keyboard.
    
    // ------------------- Constructor ---------------------
    
    /** Creates a keyboardInput object that handles prompting
      * and conversion of keyboard input.
      */   
    public KeyboardInput()
    {
    	textLine = new String();	// Create empty string.
    }
    
    
    // --------------------- Methods ----------------------
   
    /** Prompts the user to type in something from the keyboard.
      * Uses standard input to gether input.
      * @param promptText Message to prompt input with.
      */
    public void prompt(String promptText)
    {
    	System.out.print(promptText + &quot; &quot;);
    	textLine = readInput();
    }
        
    /** Extracts an integer number from the keyboard input.
      * @return Integer value extracted from keyboard input.
      */
    public int getInt()
    {
    	int intVal=0;
 
    	try
    	{
    	    intVal = new Integer(textLine).intValue();
    	}
    	catch (NumberFormatException e)
    	{
    	    System.err.println(&quot;Error converting input to integer.&quot;);
    	}
    	    
    	return intVal;  	
    }	
    	
    /** Extracts a floating point number from the keyboard input.
      * @return Floating point value extracted from keyboard input.
      */
    public float getFloat()
    {
    	float floatVal=0.0f;
    	
    	try
    	{
    	    floatVal = new Float(textLine).floatValue();
    	}
    	catch (NumberFormatException e)
    	{
    	    System.err.println(&quot;Error converting input to floating point number.&quot;);
    	}
    	    
    	return floatVal;  	
    }
    
    
    /** Extracts a double precision number from the keyboard input.
      * @return Double precision value extracted from keyboard input.
      */      
    public double getDouble()
    {
    	double doubleVal=0.0;
    	
    	try
    	{
    	    doubleVal = new Double(textLine).doubleValue();
    	}
    	catch (NumberFormatException e)
    	{
    	    System.err.println(&quot;Error converting input to double precision number.&quot;);
    	}
    	    
    	return doubleVal;  	
    }	
    
    /** Extracts a string typed in on the keyboard.
      * @return String extracted from keyboard input.
      */
    public String getString()
    {
        return textLine;
    }
    
    
    /** Extracts the first letter of a string typed in on the 
      * keyboard.
      * @return First character extracted from keyboard input.
      */
    public char getChar()
    {
    	char firstChar = ' ';
    	
    	try
    	{
    	    firstChar = textLine.charAt(0);
    	}
    	catch (StringIndexOutOfBoundsException e)
    	{
    	    System.err.println(&quot;Error finding first letter of input.&quot;);
    	}
    	    
    	return firstChar;  
    }
    
    // ------------------- Private Methods -----------------
   
    /** Returns a line of text input from standard input (usually
      * the keyboard).
      * @return Line of keyboard input.
      */     
    private String readInput()
    {
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	
    	try
    	{
    	    textLine = in.readLine();
    	}
    	catch (IOException e)
    	{
    	    System.err.println(&quot;Problem reading keyboard input&quot;);
    	    return null;
    	}
    	return textLine;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top