Problem using exceptions and looping

Thread Solved

Join Date: Oct 2007
Posts: 27
Reputation: darklich13 is an unknown quantity at this point 
Solved Threads: 0
darklich13's Avatar
darklich13 darklich13 is offline Offline
Light Poster

Problem using exceptions and looping

 
0
  #1
Jun 5th, 2008
I'm having a little trouble with one of my exceptions. The following code is just a simple division of two integers. I have built two exception to handle an InputMismatch and divide by zero. What I want to happen is when there is an exception, I want the program to return to let the user enter more data. If there is no exception, I want the program to end. The problem occurs when I enter words instead of numbers to make the exception InputMismatch work. When this happens the program goes into an infinity loop and I have to kill the program. Can somebody please look at my code and let me know what I'm doing wrong?

Also, do I need a throw for the InputMismatch?

  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3.  
  4. public class P4Exceptions
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. Scanner scan = new Scanner(System.in);
  10. int n1, n2;
  11. double r = 0;
  12. boolean continueLoop = true; // determines if more input is needed
  13.  
  14. while ( continueLoop )
  15. {
  16. try
  17. {
  18. System.out.println("Enter two numbers. I will compute the ratio:");
  19. n1 = scan.nextInt();
  20. n2 = scan.nextInt();
  21. r = (double) n1 / n2;
  22.  
  23. if (n2 == 0)
  24. throw new ArithmeticException();
  25.  
  26. System.out.println("The ratio r = " + r );
  27. continueLoop = false;
  28.  
  29. }
  30.  
  31. catch ( ArithmeticException arithmeticException )
  32. {
  33. System.out.println("There was an exception: Divide by zero. Try again\n");
  34. }
  35. catch ( InputMismatchException inputMismatchException )
  36. {
  37. System.out.println("You must enter an integer. Try again.\n");
  38. }
  39.  
  40. }
  41.  
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Problem using exceptions and looping

 
1
  #2
Jun 5th, 2008
I think it has something to do with the way the stream works--

if you call the method nextInt it will return the value on the line and attempt to the next line of code.

I'm almost certain that because the type returned was not the type that should be returned, the method nextInt threw an exception and most likely returned 0 (since the method is an int and must therefore return a valid argument). Upon doing so, the stream might not have been flushed, leaving the value on the line and for it to be re-evaluated again since the line jump was unsuccessful.

Here is the edit--


  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3.  
  4. public class P4Exceptions
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. Scanner scan = new Scanner(System.in);
  10. int n1, n2;
  11. double r = 0;
  12. boolean continueLoop = true; // determines if more input is needed
  13.  
  14. while ( continueLoop )
  15. {
  16. try
  17. {
  18. System.out.println("Enter two numbers. I will compute the ratio:");
  19. n1 = Integer.parseInt(scan.nextLine());
  20. n2 = Integer.parseInt(scan.nextLine());
  21. r = (double) n1 / n2;
  22.  
  23. if (n2 == 0)
  24. throw new ArithmeticException();
  25.  
  26. System.out.println("The ratio r = " + r );
  27. continueLoop = false;
  28.  
  29. }
  30. catch ( ArithmeticException arithmeticException )
  31. {
  32. System.out.println("There was an exception: Divide by zero. Try again\n");
  33. n1 = 0;
  34. n2 = 0;
  35. }
  36. catch ( NumberFormatException inputMismatchException )
  37. {
  38. System.out.println("You must enter an integer. Try again.\n");
  39. n1 = 0;
  40. n2 = 0;
  41. }
  42. catch(Exception e)
  43. {
  44. System.out.println(e);
  45. n1 = 0;
  46. n2 = 0;
  47. }
  48.  
  49. }
  50.  
  51. }
  52. }
Last edited by Alex Edwards; Jun 5th, 2008 at 10:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Problem using exceptions and looping

 
1
  #3
Jun 6th, 2008
I think I might have made the same mistake. The way Scanner works, is by matching patterns in some stream. When you use nextInt() it will attempt to find an integer value in the input stream. If it is successful, it will move forward in the stream. If it is not successful then it will not move forward. Thus when you input words, you remain on the same input until you call the appropriate Scanner method to match it and proceed.

So add scan.nextLine(); after the line with your message:
System.out.println("You must enter an integer. Try again.\n");

So you'd have:
  1. catch (InputMismatchException inputMismatchException)
  2. {
  3. System.out.println("You must enter an integer. Try again.\n");
  4. scan.nextLine();
  5. }

This will allow the scanner object to skip the error and move forward in the input stream. By the way, perhaps if would be best to check if n2 is zero before actually doing the division it works regardless, but it's still an unneeded calculation if there is an error.
Last edited by PoovenM; Jun 6th, 2008 at 8:44 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 27
Reputation: darklich13 is an unknown quantity at this point 
Solved Threads: 0
darklich13's Avatar
darklich13 darklich13 is offline Offline
Light Poster

Re: Problem using exceptions and looping

 
0
  #4
Jun 8th, 2008
Thank you both for your answers!! They were both really helpful!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC