Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Oh ofcourse it is silly

    but I had to do it like this .


    another question : how do I fflush stdin in java ?

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    Oh ofcourse it is silly

    but I had to do it like this .


    another question : how do I fflush stdin in java ?
    You can't flush an input stream; think of it: the moment it is flushing new bytes may come in; should it flush again and again?

    kind regards,

    Jos

  3. #23
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    	   Scanner scan = new Scanner(System.in);
    	   System.out.println("Please enter ID");
    	   tempid = scan.nextInt();   
    	   System.out.println("Enter Name");
    	   tempname =  scan.nextLine();

    output

    Please Enter ID :
    213
    Please Enter name
    Please Enter ID?


    so it takes only Integers

    typical problems in c/c++ are solved with fflush(stdin)
    what about Java ?

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    Java Code:
    	   Scanner scan = new Scanner(System.in);
    	   System.out.println("Please enter ID");
    	   tempid = scan.nextInt();   
    	   System.out.println("Enter Name");
    	   tempname =  scan.nextLine();

    output

    Please Enter ID :
    213
    Please Enter name
    Please Enter ID?


    so it takes only Integers

    typical problems in c/c++ are solved with fflush(stdin)
    what about Java ?
    No, also in C++ (or C) you can't flush input streams. If you type, say, "123" you actually type "123<enter>". The nextInt() method reads the integer 123 but leaves the <enter> character in the input buffer. The nextLine() method happily reads the empty line. You don't want that so you should get rid of the <enter> character in that input buffer. That's easy: puth another dummy nextLine() call just after the nextInt() so the <enter> character will be read from the input buffer.

    kind regards,

    Jos

  5. #25
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    in C/C++ you acctually do fflush stdin
    before you gets(); or getch();


    but yes the dummy nextline works great :) thank you very much !1

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    in C/C++ you acctually do fflush stdin
    before you gets(); or getch();
    This is what the C/C++ standards have to say about it:

    If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.
    If it was open for reading and the last operation was an input operation, the behavior depends on the specific library implementation. In some implementations this causes the input buffer to be cleared, but this is not standard behavior.
    Flushing an input stream isn't standard behaviour.

    kind regards,

    Jos

  7. #27
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Flushing an input stream isn't standard behaviour.


    Oh OK ... educative :) but it worked well you know :)


    Thank you very much for help . cheers .

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    Oh OK ... educative :) but it worked well you know :)


    Thank you very much for help . cheers .
    Sure, I bet it works on your Microsoft Windows PC box; if you port your code to any Unix/Linux box chances are high that it doesn't work. Welcome to the wonderful world of portability; this is one of the reasons that no InputStream in Java has the flush() method implemented. 'It works on my machine' isn't worth much. Standards are normative.

    kind regards,

    Jos

Page 2 of 2 FirstFirst 12

Similar Threads

  1. implements
    By danghieu in forum New To Java
    Replies: 1
    Last Post: 04-29-2010, 06:52 PM
  2. ButtonWatcher implements ActionListener
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 07:18 PM
  3. Hashtable implements ActionListener ?
    By Zayaan in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 08:49 PM
  4. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 06:19 AM
  5. How to implements cryptation in a java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •