Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 06:44 AM
MZA MZA is offline
Member
 
Join Date: Sep 2009
Posts: 5
Rep Power: 0
MZA is on a distinguished road
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:
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 11:04 AM
Senior Member
 
Join Date: Apr 2009
Posts: 2,107
Rep Power: 4
Tolls is on a distinguished road
Default
You haven't actually said what problem you're having.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-10-2010, 04:25 AM
Senior Member
 
Join Date: Mar 2009
Location: USA
Posts: 124
Rep Power: 0
Aseem is on a distinguished road
Default
use NumberFormatException which takes specific data type. If you try to enter String for number, it will throw exception.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-10-2010, 09:23 AM
thomasdevos's Avatar
Member
 
Join Date: Feb 2010
Posts: 19
Rep Power: 0
thomasdevos is on a distinguished road
Default
Hello MAZ,
acutally, you have done a great code, all you need to do is just modify this line:
Code:
					if(STOP_CHAR.equals(in.next(STOP_CHAR))){
into
Code:
					if(STOP_CHAR.equals(in.next())){
you world is in peace.

here is some test on your program:
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:
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:02 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org