Results 1 to 3 of 3
  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default how to flush the buffer?

    I describe my problem in the source code below.

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Scanner;
    public class DivideByZeroWithExceptionHandling  {
    	private static int quotient (int numerator , int denominator) throws ArithmeticException
    		return numerator / denominator;
    	public static void main (String argv[] )
    		Scanner input = new Scanner(;
    		boolean loop = true;
    			try {
    				System.out.print("Input numerator:");
    				int numerator = input.nextInt();
    				System.out.print("Input denominator:");
    				int denominator = input.nextInt();
    				System.out.printf("%d / %d = %d\n", numerator, denominator,  DivideByZeroWithExceptionHandling.quotient(numerator, denominator) );
    				loop = false;
    			catch (ArithmeticException e)
    				System.out.println("Zero is an invalid denominator.Please try again.\n");
    			catch (InputMismatchException e)
    				System.out.println("You must Enter integers.Try again.\n");
    			}  [COLOR="GREEN"]// when this exception was caught,  the program went into an infinite loop
    			    // That the sentence "You must Enter integers.Try again" loops .[/COLOR]
    		} while(loop);

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Input streams can't be flushed; only output streams can do that. A Scanner objects has some methods to skip some input, but the easiest way out is to read the entire offending line and try again with new input (instead of the input that made the Scanner throw an exception); a Scanner can also test whether or not an int can be read without throwing an exception. Read its API documentation.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    You can flush the buffer (the input stream) by reading a line from it.

    [Edit] though I agree with Jos - it's not really a buffer and you aren't really flushing it. More importantly I agree that this can be done without exceptions. But if you really must, then a judiciously placed readLine() should do the job.
    Last edited by pbrockway2; 08-30-2011 at 10:40 AM.

Similar Threads

  1. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 07:48 PM
  2. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 3
    Last Post: 01-29-2011, 05:36 AM
  3. GZIPOutputstream flush
    By Nardian in forum Networking
    Replies: 4
    Last Post: 09-25-2010, 05:09 PM
  4. How to flush gui from using old data
    By ShinTec in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2010, 02:18 PM
  5. flush method
    By bugger in forum New To Java
    Replies: 0
    Last Post: 11-11-2007, 07:39 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