Page 1 of 2 12 LastLast
Results 1 to 10 of 18

How to clear buffer in Java?


This discussion is about "How to clear buffer in Java?" in the "Programming" forums.
i have a problem with my Java program.. i need to input 2 Strings and 1 char using a Scanner object named console.. so i ...

  1. #1
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default How to clear buffer in Java?


    i have a problem with my Java program..
    i need to input 2 Strings and 1 char using a Scanner object named console..
    so i use
    Code:
    Scanner console = new Scanner(System.in);
    ...
    String First = console.nextLine();
    char Middle = console.next().charAt(0);
    String Last = console.nextLine();
    now, it works fine the first time, but when I want to input again, it skips First and asks for Middle right away (especially when First or Last has blank spaces).
    i read about clearing input buffers in C when I had a similar problem, but how do I do it in Java?

  2. #2
    Senior Member
    Join Date
    Aug 2005
    Gender
    Male
    Posts
    553

    Default Re: How to clear buffer in Java?

    Try to issue a read on the System.in InputStream.
    Or you could call mark() to all available() buffer and then reset() on it to flush the data of the default InputStream.

  3. #3
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default Re: How to clear buffer in Java?

    unsaon na bro? thanks..

  4. #4
    Senior Member
    Join Date
    Aug 2005
    Gender
    Male
    Posts
    553

    Default Re: How to clear buffer in Java?

    Code:
    while (System.in.read() != -1)
      continue;

  5. #5
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default Re: How to clear buffer in Java?

    so i just add that code somewhere, and it will clear the buffer?

  6. #6
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default Re: How to clear buffer in Java?

    just tried what you suggested, doesn't work though...
    thanks btw..

  7. #7
    Senior Member
    Join Date
    Aug 2005
    Gender
    Male
    Posts
    553

    Default Re: How to clear buffer in Java?

    The code above will remove all pending characters from System.in .
    But maybe this is not what you need in your program.

    Could you post a compilable subset of your program for us to test and debug on our side.

  8. #8
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default Re: How to clear buffer in Java?

    thanks for helping me bai..
    here is a snippet of the method that does the inputting
    Code:
    System.out.print("Enter first name: ");
    customer.setFirstName(console.nextLine());
    		
    System.out.print("Enter middle initial: ");
    customer.setMI(console.nextLine().charAt(0));
    		
    System.out.print("Enter last name: ");
    customer.setLastName(console.nextLine());
    		
    double balTemp;
    do{
      System.out.print("Enter account balance: ");
      balTemp = console.nextDouble();
      if(balTemp >= 5000)
        break;
      System.out.print("5000.00 is required. ");
    }while(balTemp < 5000);
    the First and Last name need to be able to accept strings with spaces..

  9. #9
    Senior Member
    Join Date
    Aug 2005
    Gender
    Male
    Posts
    553

    Default Re: How to clear buffer in Java?

    use next() instead of nextLine()

  10. #10
    C.I.A.
    Join Date
    Oct 2004
    Gender
    Male
    Posts
    3,184
    Blog Entries
    1

    Default Re: How to clear buffer in Java?

    afaik, next() doesn't accept spaces.. i tried it and it messed things up. i'll try again though...
    edit: just used next(), InputMismatchException is thrown because nextDouble() gets a string when i enter a blank space..

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. how to print jtextarea in java
    By jairoh_ in forum Programming
    Replies: 5
    Last Post: 10-05-2012, 11:38 PM
  2. Replies: 96
    Last Post: 12-14-2008, 07:55 PM
  3. How to cook proper in Hotdogs!!!
    By kyme in forum Food & Dining
    Replies: 34
    Last Post: 12-14-2006, 07:13 PM
  4. How to have acess in the Mature Discussions...
    By emokidstuff in forum Support Center
    Replies: 2
    Last Post: 06-15-2005, 01:07 PM
  5. How to change ROM in SAP 1(HTC Tanager)
    By shadowkenshin in forum Gizmos & Gadgets (Old)
    Replies: 0
    Last Post: 04-21-2005, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top