Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2013, 04:08 PM
Bradyarch Bradyarch is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 Bradyarch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 8 sec
Reputation Power: 0
[Homework] Do-While Try-Catch exception repeating

Hello everyone, thank you for opening my topic.

I am working on a very simple Exceptions-testing class.

The program asks for Numerator and Denominator and then divides them and returns the quotient.

I am trying to catch 2 exceptions. One of them is InputMismatchException (for non-numbers) and one of them is ArithmeticException (for dividing by 0).

I am using Do-While so that when it catches, it will keep asking you until you get it correct.
My issue at hand is this: when it catches the InputMismatchException, it constantly repeats the error message and the "Enter Numerator:" to no avail.

This is because I have the Do-While being stopped after the Try has tried to get a numerator and denominator and carries out the rest, but the InputMismatchException is caught before it all executes. My code will help you understand better:

import java.util.Scanner;
import java.util.InputMismatchException;

public class Quotient {

public static void main(String[] args) {

int dividend, divisor;
Scanner scan = new Scanner(System.in);
int keepTrying = 1;
final int STOP_TRYING = 2;
final int KEEP_TRYING = 1;

do
{
try
{
System.out.print ("Enter the numerator: ");
dividend = scan.nextInt();
System.out.print ("Enter the denominator: ");
divisor = scan.nextInt();

System.out.println ("The fraction " + dividend + "/" +
divisor + " is equal to " + intQuotient(dividend, divisor) + ".");
keepTrying = STOP_TRYING;
}
catch (ArithmeticException e)
{
System.out.println("You cannot divide by zero!");
}
catch (InputMismatchException e)
{
System.out.println("You cannot divide by non-numbers or a number higher than 2,147,483,647.");
}
}
while(keepTrying==KEEP_TRYING);


}

// Converts a fraction to a decimal by dividing the numerator
// by the denominator
private static int intQuotient (int n, int d)
{
int q = n / d;

return q;
}

}


Thank you all for reading. I tried to post the Pastebin link but, being a new account, I cannot post URLs. I look forward to all your responses!!!
-Bradyarch

Reply With Quote
  #2  
Old January 30th, 2013, 06:10 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,040 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 36 m 54 sec
Reputation Power: 346
Please edit your post and wrap the code in code tags to make the code easier to read.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Do-While Try-Catch exception repeating

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap