Results 1 to 4 of 4
  1. #1
    MZA
    MZA is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    You haven't actually said what problem you're having.

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    use NumberFormatException which takes specific data type. If you try to enter String for number, it will throw exception.

  4. #4
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    Hello MAZ,
    acutally, you have done a great code, all you need to do is just modify this line:
    Java Code:
    					if(STOP_CHAR.equals(in.next(STOP_CHAR))){
    into
    Java Code:
    					if(STOP_CHAR.equals(in.next())){
    you world is in peace.

    here is some test on your program:
    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]
    and below is runnable code modified from yours:
    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
    i hope you can learn something from by comparation.
    good luck
    i 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

  1. Exception Handling Related
    By dnzzn in forum New To Java
    Replies: 9
    Last Post: 09-29-2009, 09:45 AM
  2. Exception handling and logging
    By jurka in forum New To Java
    Replies: 8
    Last Post: 09-03-2008, 07:07 PM
  3. JAXP exception handling
    By jovenky in forum Advanced Java
    Replies: 0
    Last Post: 05-27-2008, 01:37 PM
  4. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  5. Jstl Exception Handling
    By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-18-2007, 06:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •