To input a character in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiv4singh
    New Member
    • May 2007
    • 3

    #1

    To input a character in Java

    Hi There
    Can anyone write me a program in which a character input is taken from console? If I am executing a program using do..while() loop. Is there any way through which I could clear the contente of console each time when execution takes place.

    Thanks n Regards,
    Shiv Kumar Singh
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shiv4singh
    Hi There
    Can anyone write me a program in which a character input is taken from console? If I am executing a program using do..while() loop. Is there any way through which I could clear the contente of console each time when execution takes place.

    Thanks n Regards,
    Shiv Kumar Singh
    Write the code yourself. We are only here to help you write the code. Clearing the console is not something you want to do now in java. Your best bet is to write yourself a clrscr method which simply prints blank lines to the console until there's nothing showing.

    Comment

    • shiv4singh
      New Member
      • May 2007
      • 3

      #3
      Well I wrote the code and used read() method but it was generating IOException. Below is the code that I had coded :

      import java.io.*;
      import java.io.Reader;
      public class test
      {
      public static void main() //throws Exception
      {
      BufferedReader buf = new BufferedReader( new InputStreamRead er(System.in));
      System.out.prin tln("Enter a Character :");
      try
      {
      //char ch= (char)(System.i n.read());
      char ch = (char)(buf.read ());
      }
      catch(IOExcepti on ei)
      {
      System.out.prin tln("Error");
      }
      }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Use Scanner for taking input. There's a thread in this forum now that's discussing that.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Instead of just printing "Error" do this: "e.printStackTr ace()" and see the reason
          for that ugly exception in all its glory.

          kind regards,

          Jos

          Comment

          • akash banasha
            New Member
            • Jun 2007
            • 1

            #6
            what ever u hav writtn is mere bullshit ......... stop giving nymore reply

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by akash banasha
              what ever u hav writtn is mere bullshit ......... stop giving nymore reply
              Pardon me, but I haven't seen any 'mere bullshit' in this thread until you replied.
              The OP is learning and s/he has been given some advice. Everyone is free to
              reply in any public thread, you can't make them stop nor can you forbid such
              actions. please refrain from such replies in the near future; thanks.

              kind regards,

              Jos

              Comment

              Working...