Adding Exceptions

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1128 Views - Last Post: 27 March 2011 - 10:37 PM Rate Topic: -----

#1 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Adding Exceptions

Posted 27 March 2011 - 03:15 PM

I have to add to Exceptions into my project, but am unsure how to go about doing it, any assistance is appreciated.





import java.util.Scanner;
	import java.text.*;
	public class CertificateofDeposit5
	{
	public static void main(String[] args)
	{
	        Scanner console = new Scanner(System.in);
	         
	    double contribution=0;
		 double Apr=0;
		          
	do
	{
	System.out.println("Please enter what your able to contribute. ");
	contribution = console.nextFloat();
	if (contribution <= 99)
	{
	System.out.println("Current banking rules don't allow this amount. ");
	}
	}while(contribution <= 99);
	
	
	do
	{
	System.out.println("Current Market Annual Interest Rate. ");
	Apr = console.nextFloat();
	if(Apr <= 0.0 || Apr >= 10)
	{
	System.out.println("Input not accepted, please enter amount greater that 0% and less than 10%. ");
	}
	}while(Apr <= 0.0 || Apr >= 10);
	
	
	
	         
	        System.out.println("Number of Years Planned Investment. ");
	        int years = console.nextInt();
	        System.out.println();
	        printTables(years, contribution, Apr);
	        System.out.println();
	}
	public static double calcInt
	                (double balance, double rate)
	{
	                double xRate = (rate / 100);
	                double interestTotals = balance * xRate;
	        balance = balance + interestTotals;
	                return interestTotals;
	}
	public static void printTables(int numberRows, double balance, double rate)
	{
	                double compondInterest=0;
	        System.out.println("Year#\tCash\t\tInterest\tTotalCash");
	        System.out.println("----- \t----\t\t-------- \t----------");
	                for (char i = 1; i <= numberRows; i++)
	{
	        compondInterest = calcInt(balance, rate);
	        printRow(i, balance, compondInterest);
	        balance = balance + compondInterest;
	}
	}
	public static void printRow(char rowNum, double bal, double interest)
	 
	{
	                double newBalance = bal + interest;
	        String StringBal = roundTo2Dec(bal);
	        String StringInt = roundTo2Dec(interest);
	        String StringNewBal = roundTo2Dec(newBalance);
	        System.out.println(rowNum + "\t" + StringBal + "\t" + StringInt + "\t\t" + StringNewBal);
	}
	public static String roundTo2Dec(double value)
	{
	        DecimalFormat Currency = new DecimalFormat("$#,###.0#");
	        String exactFormat = Currency.format(value);
	                return exactFormat;}
	 
	    }


This post has been edited by pbl: 27 March 2011 - 03:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding Exceptions

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Adding Exceptions

Posted 27 March 2011 - 03:20 PM

After10 posts it is about time your learn about :code:
Was This Post Helpful? 2
  • +
  • -

#3 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Adding Exceptions

Posted 27 March 2011 - 03:23 PM

You are very unclear with what you want. Do you want to intentionally throw exceptions? Do you want to make your own type of exception? Or, do you want to catch an exception that is already including in the Java API?

From what I can see, none of your code will throw exceptions.

If you want to throw an exception for error'd input do:

     throw new RuntimeException("Input isn't blah blah blah.");


Was This Post Helpful? 1
  • +
  • -

#4 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 03:29 PM

View PostManbearpig101, on 27 March 2011 - 03:23 PM, said:

You are very unclear with what you want. Do you want to intentionally throw exceptions? Do you want to make your own type of exception? Or, do you want to catch an exception that is already including in the Java API?

From what I can see, none of your code will throw exceptions.

If you want to throw an exception for error'd input do:

     throw new RuntimeException("Input isn't blah blah blah.");



I have to add any type of exception
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Adding Exceptions

Posted 27 March 2011 - 03:31 PM

Generally, many types of exeption are used in java, one of them is try..catch...finally:
Its style is:
try{
//put your code to throw exception when it occur
.
.
}
catch(exception e){
//action to be taken when error occurs
}

Was This Post Helpful? 1
  • +
  • -

#6 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 03:31 PM

View Postmwharyjr, on 27 March 2011 - 03:29 PM, said:

View PostManbearpig101, on 27 March 2011 - 03:23 PM, said:

You are very unclear with what you want. Do you want to intentionally throw exceptions? Do you want to make your own type of exception? Or, do you want to catch an exception that is already including in the Java API?

From what I can see, none of your code will throw exceptions.

If you want to throw an exception for error'd input do:

     throw new RuntimeException("Input isn't blah blah blah.");



I have to add any type of exception

whichever is the easiest to put into this
Was This Post Helpful? 0
  • +
  • -

#7 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Adding Exceptions

Posted 27 March 2011 - 03:33 PM

Do you want to throw an exception, or catch an exception while it is happening?
Was This Post Helpful? 1
  • +
  • -

#8 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 03:36 PM

View PostManbearpig101, on 27 March 2011 - 03:33 PM, said:

Do you want to throw an exception, or catch an exception while it is happening?


Like this, how would I throw an exception if they enter a contribution lower than $100 and have them re-enter an amount??

try
{		          
	do
	{
	System.out.println("Please enter what your able to contribute. ");
	contribution = console.nextFloat();
	if (contribution <= 99)
	{
	System.out.println("Current banking rules don't allow this amount. ");
	}
	}while(contribution <= 99);
	
catch(exception e)
{


Was This Post Helpful? 0
  • +
  • -

#9 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Adding Exceptions

Posted 27 March 2011 - 03:43 PM

You could just do:

But I don't get the purpose..

try {
     do {
          System.out.println("Please enter what you're able to contribute. ");
          contibution = console.nextFloat();
          if (contribution <= 99) {
               throw new RuntimeException("Current banking rules don't allow this limit!");
          }
     } while (contribution <= 99);
} catch (Exception ex) {
    System.out.println(ex.getMessage());
}


Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Adding Exceptions

Posted 27 March 2011 - 03:47 PM

I dont see the need of using exception here. Just using if ..else is enough!
Was This Post Helpful? 1
  • +
  • -

#11 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 03:48 PM

View PostManbearpig101, on 27 March 2011 - 03:43 PM, said:

You could just do:

But I don't get the purpose..

try {
     do {
          System.out.println("Please enter what you're able to contribute. ");
          contibution = console.nextFloat();
          if (contribution <= 99) {
               throw new RuntimeException("Current banking rules don't allow this limit!");
          }
     } while (contribution <= 99);
} catch (Exception ex) {
    System.out.println(ex.getMessage());
}



Thanks alot, now that I have the basic concept, I can add the others. Don't ask me, I don't know the purpose either. I been trying to get help from my professor for 6 days now and he never returned my emails. I want to thank everyone that has assisted me. I made sure to hit the + on all responces.
Was This Post Helpful? 0
  • +
  • -

#12 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 08:30 PM

My Exceptions are ignoring my if statement, what did I do wrong, or what didn't I do. When I enter $99 it says "Current banking rules don't allow this limit!" And goes to the APR, It shouldn't go to the APR until I enter a Starting balance over $100. Thanks

import java.util.Scanner;
import java.text.*;
public class CertificateofDeposit5
{
public static void main(String[] args)
{

		Scanner console = new Scanner(System.in);
	         
	  				double contribution=0;
		 				double Apr=0;	
		 

	try 
	{
			do 
			{
				System.out.println("Please enter what you're able to contribute. ");
				contribution = console.nextFloat();
			if (contribution <= 99)
         {
					throw new RuntimeException("Current banking rules don't allow this limit!");
					}
					

			} while (contribution <= 99);
			} catch (Exception ex) 
			{
				System.out.println(ex.getMessage());
			}	


	try
	{
			do
			{
				System.out.println("Current Market Annual Interest Rate. ");	
				Apr = console.nextFloat();
			if(Apr <= 0.0 || Apr >= 10)
			{
					throw new RuntimeException("Input not accepted, please enter amount greater that 0% and less than 10%. ");
			}
			}while(Apr <= 0.0 || Apr >= 10);
			} catch (Exception ex)
			{
; 
				System.out.println(ex.getMessage());
			}	
				
				System.out.println("Number of Years Planned Investment. ");
	        int years = console.nextInt();
	        System.out.println();
	        printTables(years, contribution, Apr);
	        System.out.println();
	}
	public static double calcInt
	                (double balance, double rate)
	{
	                double xRate = (rate / 100);
	                double interestTotals = balance * xRate;
	        balance = balance + interestTotals;
	                return interestTotals;
	}
	public static void printTables(int numberRows, double balance, double rate)
	{
	                double compondInterest=0;
	        System.out.println("Year\tCash\t\tInterest\tTotalCash");
	        System.out.println("-----\t----\t\t------- \t----------");
	                for (char i = 1; i <= numberRows; i++)
	{
	        compondInterest = calcInt(balance, rate);
	        printRow(i, balance, compondInterest);
	        balance = balance + compondInterest;
	}
	}
	public static void printRow(char rowNum, double bal, double interest)
	 
	{
	                double newBalance = bal + interest;
	        String StringBal = roundTo2Dec(bal);
	        String StringInt = roundTo2Dec(interest);
	        String StringNewBal = roundTo2Dec(newBalance);
	        System.out.println(rowNum + "\t" + StringBal + "\t" + StringInt + "\t\t" + StringNewBal);
	}
	public static String roundTo2Dec(double value)
	{
	        DecimalFormat Currency = new DecimalFormat("$#,###.0#");
	        String exactFormat = Currency.format(value);
	                return exactFormat;}
	 
	    }



Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: Adding Exceptions

Posted 27 March 2011 - 08:34 PM

Moved to Java help. Please reserve the Java Programmers forum for topical, non-help related discussion.

The reason this is happening is b/c your Exception is breaking out of the loop, and going to the catch block. It defeats the purpose to have Exception handling and a loop here. I would just get rid of the Exception handling altogether here.
Was This Post Helpful? 1
  • +
  • -

#14 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: Adding Exceptions

Posted 27 March 2011 - 08:37 PM

View Postmacosxnerd101, on 27 March 2011 - 08:34 PM, said:

Moved to Java help. Please reserve the Java Programmers forum for topical, non-help related discussion.

The reason this is happening is b/c your Exception is breaking out of the loop, and going to the catch block. It defeats the purpose to have Exception handling and a loop here. I would just get rid of the Exception handling altogether here.


I would, but it has to have two exceptions, and I don't know where else to put them
Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Adding Exceptions

Posted 27 March 2011 - 08:50 PM

There is no need to use exceptions in this case. All you are doing is testing a condition and printing an error message based on the results of the test. That can be done with a simple if statement.

But it would actually make more sense to prompt for those inputs inside while loops that repeatedly prompt the user for corrected input when the user supplies invalid data.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2