Results 1 to 7 of 7
Thread: Average with Exception Handling
- 10-04-2010, 01:25 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Average with Exception Handling
I believe all of my logic is sound, but syntax is off a bit...
It won't compile and I am not sure how to fix it.
-ask for total numbers
-for loop to input total numbers
-1)input first number
-2)throw exception "must be positive"
-3)if negative > back to 1
-4)if no exception > calculate average
This is my first program using exception handling, so go easy :).
Java Code:import java.util.Scanner; public class Average { public static void main (String[] args) { try { int n, total, sum, average; Scanner keyboard = new Scanner(System.in); System.out.println("Enter total numbers to be entered"); total = keyboard.nextInt( ); for (i=0;i<total;i++) { System.out.println("Enter N"); n = keyboard.nextInt( ); if (n<0) throw new BadInput("N must be positive"); sum = n + sum; average = sum/total; System.out.println("Average is " + average); } } catch (BadInput e) { String message = e.getMessage(); System.out.println(message); anotherChance(); } } public static void anotherChance() { System.out.println("Enter N"); n = keyboard.nextInt(); } public static void BadInput() { super("BAD INPUT; must be positive!"); } }
- 10-04-2010, 02:14 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
What is the exact compiler message?
Note that BadInput must be a class defined somewhere. Your "public static void BadInput()" defines a method.
Why use exceptions to control the flow of your program? You could do something along the lines of:
Java Code:int processed = 0 while(processed<total) { // get number // if good: update sum and average. and increment processed }
- 10-04-2010, 02:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Do you really want to print the sum and average every time around the loop? If not, then just update sum inside the loop and move the calculation and display of average to after the loop has finished.
- 10-04-2010, 02:32 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Still am a little confused on the getMessage and BadException errors I'm getting.
Java Code:import java.util.Scanner; public class Average { public static void main (String[] args) { try { int n, i, total, sum=0, average; Scanner keyboard = new Scanner(System.in); System.out.println("Enter total numbers to be entered"); total = keyboard.nextInt( ); for (i=0;i<total;i++) { System.out.println("Enter N"); n = keyboard.nextInt( ); if (n<0) throw new BadInput("N must be positive"); } sum = n + sum; average = sum/total; System.out.println("Average is " + average); } catch (BadInput e) { String message = e.getMessage(); System.out.println(message); anotherChance(); } } public static void anotherChance() { int n; Scanner keyboard = new Scanner(System.in); System.out.println("Enter N"); n = keyboard.nextInt(); } private static class BadInput { public BadInput(String string) { } } }
- 10-04-2010, 04:07 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
NVM, got it.
I mixed up 2 words.
- 10-04-2010, 02:02 PM #6
Cross posted at Average with Exception Handling - Page 2 - Java
- 10-04-2010, 11:27 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Exception Handling
By liljester in forum New To JavaReplies: 4Last Post: 06-21-2010, 03:09 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM -
JDBC - Exception handling
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 04:00 PM -
JSTL Exception Handling
By chaatf in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-18-2007, 02:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks