Java equivalent of cin

Discussion in 'Java' started by Gregory W. Ernest, Jan 18, 2005.

  1. I am new to Java programming and I was wondering if anyone can tell me the
    java equivalent to a cin>>...; that is normally used in c++. I want to get
    user input and automatically assign it to a variable. So in other words what
    would be the equivant java statement of this c++ statement:

    1: cout<<"Enter a number.";
    2: cin>>x; //This is what I really want to know. I already know the java
    equivalent of the first statement.
     
    Gregory W. Ernest, Jan 18, 2005
    #1
    1. Advertising

  2. Gregory W. Ernest

    Harish Guest

    yes System.out is the standard output of a java program.
    and System.in is the standard input.
    so try System.in.read...

    "Gregory W. Ernest" <> wrote in message
    news:VR_Gd.7784$...
    >I am new to Java programming and I was wondering if anyone can tell me the
    > java equivalent to a cin>>...; that is normally used in c++. I want to get
    > user input and automatically assign it to a variable. So in other words
    > what
    > would be the equivant java statement of this c++ statement:
    >
    > 1: cout<<"Enter a number.";
    > 2: cin>>x; //This is what I really want to know. I already know the java
    > equivalent of the first statement.
    >
    >
    >
     
    Harish, Jan 18, 2005
    #2
    1. Advertising

  3. Gregory W. Ernest

    moongate Guest

    The main difference being that System.in belongs to class InputStream,
    which does not provide functionaly as rich as C++'s "cin"; instead, it
    focuses on low-level, byte-oriented input operations. In most cases,
    you will want to wrap System.in with filter objects that augment its
    functionality (stream wrapping basically works as in C++). A common
    wrapping would be something like

    BufferedReader dis = new BufferedReader(new
    InputStreamReader(System.in))

    where BufferedReader augmented functionality allows you, for example,
    to read character input line by line:

    String line = bin.readLine();

    Look for other wrappings based on your specific needs. You might want
    to take a look at basic principles of Java I/O (e.g., the difference
    between "Reader/Writer" and "Stream" classes) in a tutorial or a book.
    MC
     
    moongate, Jan 18, 2005
    #3
  4. Gregory W. Ernest

    gstkein

    Joined:
    Aug 3, 2008
    Messages:
    1
    A Java Cin like class

    import java.io.*;


    /**
    * @author gstkein
    *
    */
    public class Cin {

    public static int Int(){

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int intInput=-1;
    boolean invalido = true;

    while (invalido) {
    invalido=false;
    try {
    intInput = Integer.parseInt(br.readLine());
    } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    invalido=true;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    invalido=true;
    }
    }
    return intInput;
    }

    public static String String() {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input="";

    try {
    input = (br.readLine());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return input;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.print("Type your name: ");
    String name = Cin.String();
    System.out.print("Type a number: ");
    int number = Cin.Int();
    System.out.println(name);
    System.out.println(number);



    }

    }
     
    Last edited: Aug 3, 2008
    gstkein, Aug 3, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,109
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,178
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,705
    Alex Vinokur
    Mar 6, 2005
  4. Replies:
    13
    Views:
    2,745
    Arne Vajhøj
    Mar 18, 2008
  5. Fernando
    Replies:
    4
    Views:
    1,687
    Fernando
    Nov 16, 2011
Loading...

Share This Page