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

    Default how to flush the buffer?

    hi,all
    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(System.in);
    		boolean loop = true;
    		
    		do{
    			
    		
    			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 online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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 11:40 AM.

Similar Threads

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