Results 1 to 4 of 4
Thread: Exception Handling help
- 02-09-2010, 06:44 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
Exception Handling help
Hello all.
I have to trying to write a program that takes user-input floating point numbers and adds them together. If the user enters a value that isn't a number, it gives them a second chance to enter some, and if they don't it then quits reading input. And this is where I'm goofed. My exception handling of the improper inputs is messing me up. Any advice on a good way to fix them?
Here's my code:
Java Code:import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class RealAdder { private static ArrayList<Double> numArray = new ArrayList<Double>(); private static double sum = 0;//compute sum() private static final String STOP_CHAR = "~"; private static final int EXIT_NO_VALUES = 0; private static final int MAX_ATTEMPTS = 2; private static int attempts = 0; private static int an = 0; //Last array; /** * @param args * Usage [] * @return */ public static void main(String[] args) { // Get user input values getNumbers(); // Compute the sum of the numbers computeSum(); // Output the sum printSum(); } /** *@param InputMismatchException * * */ private static void getNumbers() { boolean done = false; while (!done){ Scanner in = new Scanner(System.in); try{ System.out.println("Enter a floating-point variable. Type '~' when finished. "); System.out.println("try1"); while(in.hasNextDouble()){ System.out.println("in.hasNextDouble"); numArray.add(in.nextDouble()); an++; } try{ System.out.println("try2"); if(STOP_CHAR.equals(in.next(STOP_CHAR))){ System.out.println("try2/if"); System.out.println("Stop Char"); done=true; } } finally{ System.out.println("Finally"); in.close(); if(attempts>=MAX_ATTEMPTS){ System.out.println("Maximum attempts has been reached. Goodbye."); System.exit(0); } } } catch(InputMismatchException exception){ System.out.println("Catch statement 1 activated"); System.out.println("Not a floating-point variable"); attempts++; } catch(IndexOutOfBoundsException exception){ System.out.println("Catch statement 2 activated"); attempts++; } } } private static void computeSum() { System.out.println("ComputeSum"); System.out.println("NumArraySize: " +an); if(numArray.isEmpty()){ System.out.println("Array Empty"); System.exit(EXIT_NO_VALUES); } for(int a1 = 0; a1 < numArray.size(); a1++){ sum = sum + numArray.get(a1); } } private static void printSum() { System.out.println("Sum: " +sum +"\nArrayList:" +numArray); }//End printSum }//End Main
- 02-09-2010, 11:04 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
You haven't actually said what problem you're having.
- 02-10-2010, 04:25 AM #3
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
use NumberFormatException which takes specific data type. If you try to enter String for number, it will throw exception.
- 02-10-2010, 09:23 AM #4
Hello MAZ,
acutally, you have done a great code, all you need to do is just modify this line:
intoJava Code:if(STOP_CHAR.equals(in.next(STOP_CHAR))){
you world is in peace.Java Code:if(STOP_CHAR.equals(in.next())){
here is some test on your program:
and below is runnable code modified from yours:Java Code:Enter a floating-point variable. Type '~' when finished. try1 1 in.hasNextDouble 2 in.hasNextDouble 3 in.hasNextDouble 4 in.hasNextDouble ~ try2 try2/if Stop Char Finally ComputeSum NumArraySize: 4 Sum: 10.0 ArrayList:[1.0, 2.0, 3.0, 4.0]
i hope you can learn something from by comparation.Java Code:import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class ExceptionHandle { private static ArrayList<Double> numArray = new ArrayList<Double>(); private static double sum = 0;//compute sum() private static final String STOP_CHAR = "~"; private static final int EXIT_NO_VALUES = 0; private static final int MAX_ATTEMPTS = 2; private static int attempts = 0; private static int an = 0; //Last array; /** * @param args * Usage [] * @return */ public static void main(String[] args) { // Get user input values getNumbers(); // Compute the sum of the numbers computeSum(); // Output the sum printSum(); } /** *@param InputMismatchException * * */ private static void getNumbers() { boolean done = false; while (!done){ Scanner in = new Scanner(System.in); try{ System.out.println("Enter a floating-point variable. Type '~' when finished. "); System.out.println("try1"); while(in.hasNextDouble()){ System.out.println("in.hasNextDouble"); numArray.add(in.nextDouble()); an++; } try{ System.out.println("try2"); if(STOP_CHAR.equals(in.next())){ System.out.println("try2/if"); System.out.println("Stop Char"); done=true; } } finally{ System.out.println("Finally"); in.close(); if(attempts>=MAX_ATTEMPTS){ System.out.println("Maximum attempts has been reached. Goodbye."); System.exit(0); } } } catch(InputMismatchException exception){ System.out.println("Catch statement 1 activated"); System.out.println("Not a floating-point variable"); attempts++; } catch(IndexOutOfBoundsException exception){ System.out.println("Catch statement 2 activated"); attempts++; } } } private static void computeSum() { System.out.println("ComputeSum"); System.out.println("NumArraySize: " +an); if(numArray.isEmpty()){ System.out.println("Array Empty"); System.exit(EXIT_NO_VALUES); } for(int a1 = 0; a1 < numArray.size(); a1++){ sum = sum + numArray.get(a1); } } private static void printSum() { System.out.println("Sum: " +sum +"\nArrayList:" +numArray); }//End printSum }//End Main
good lucki hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project
Similar Threads
-
Exception Handling Related
By dnzzn in forum New To JavaReplies: 9Last Post: 09-29-2009, 09:45 AM -
Exception handling and logging
By jurka in forum New To JavaReplies: 8Last Post: 09-03-2008, 07:07 PM -
JAXP exception handling
By jovenky in forum Advanced JavaReplies: 0Last Post: 05-27-2008, 01:37 PM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM -
Jstl Exception Handling
By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 07-18-2007, 06:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks