Taking input in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Monu2301
    New Member
    • May 2007
    • 1

    Taking input in java

    hello!
    I am new to java but as compared to any other language like c or c++
    taking input in java is difficult but I tried using Keyborad.readIn t(); for reading the inputs given by the keyborad,but still not able to properly execute it.
    Can anyone help me regarding taking input in java'
    Thanks in advance!!!!!!!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Monu2301
    hello!
    I am new to java but as compared to any other language like c or c++
    taking input in java is difficult but I tried using Keyborad.readIn t(); for reading the inputs given by the keyborad,but still not able to properly execute it.
    Can anyone help me regarding taking input in java'
    Thanks in advance!!!!!!!
    If you are using 1.5 or higher (you should be), have a look at how to use the Scanner to take input from users

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Welcome to TSDN.

      you can also use like ....

      [code=java]
      InputStreamRead er in = new InputStreamRead er(System.in);
      //Here System.in is the standard input stream and that is keyboard.
      [/code]

      kind regards.
      dmjpro.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        Welcome to TSDN.

        you can also use like ....

        [code=java]
        InputStreamRead er in = new InputStreamRead er(System.in);
        //Here System.in is the standard input stream and that is keyboard.
        [/code]

        kind regards.
        dmjpro.
        That doesn't help you much, you still have to do the scanning yourself that way.
        As r035198x already wrote: use a scanner to do the dirty work:[code=java]
        Scanner in= new Scanner(System. in);
        if (in.hasNextInt( ))
        System.out.prin tln("next int in stream: "+in.nextInt()) ;
        else
        System.out.prin tln("no int as the current token in stream");[/code]

        You have to upgrade to at least Java 1.5 (1.6 is better) to be able to use a Scanner.

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          so JOASH Scanner is much more better than InputStreamRead er????

          kind regards.
          dmjpro.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            so JOASH Scanner is much more better than InputStreamRead er????

            kind regards.
            dmjpro.
            Erm, yes, that's what I wrote basically in my previous reply. A Scanner still isn't
            a full blown lexical analyzer but it's much better than any home brew tokenizer
            based on an InputStream or Reader.

            kind regards,

            Jos

            Comment

            • RJaya
              New Member
              • Nov 2007
              • 1

              #7
              I am new to java.Programmin g for input in java is difficult but I tried using Keyborad.readIn t(); It is appeared to read it but System.out.prin tln gives a totally different value. Can anyone help me regarding taking input in java'
              Thanks in advance

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by RJaya
                I am new to java.Programmin g for input in java is difficult but I tried using Keyborad.readIn t(); It is appeared to read it but System.out.prin tln gives a totally different value. Can anyone help me regarding taking input in java'
                Thanks in advance
                Have you read the previous replies? Use a Scanner object to do the dirty work:
                it can read several types of numbers and strings and (almost) whatever you want.

                kind regards,

                Jos

                Comment

                Working...