943,960 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2155
  • Java RSS
Jun 5th, 2008
0

Problem using exceptions and looping

Expand Post »
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?

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
darklich13 is offline Offline
27 posts
since Oct 2007
Jun 5th, 2008
1

Re: Problem using exceptions and looping

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--


Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 6th, 2008
1

Re: Problem using exceptions and looping

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:
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Jun 8th, 2008
0

Re: Problem using exceptions and looping

Thank you both for your answers!! They were both really helpful!!
Reputation Points: 10
Solved Threads: 0
Light Poster
darklich13 is offline Offline
27 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Null Pointer
Next Thread in Java Forum Timeline: please help me solve this! How to search in java using MYSQL database?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC