pgmtkl
asked on
try/catch
I am working on this section of code below to get the try and catch to run properly in a gui program. The program ran fine when i had one section and then i add the rate and term and receive an error that i need try with catch, which i have. Am i missing something or is the order wrong? thanks
public static void main(String[] args) throws Exception
{
int val = 0;
String amount;
String rate;
String term;
int response = 0;
boolean valid = true;
do{
// The showInoutDialog is a method of JOptionPane and its return value is a string.
// Since the return value is a string, it is necessary that "grade" should be of a string
// datatype too.
amount = JOptionPane.showInputDialo g(null,"Pl ease enter amount to be financed:", " ", JOptionPane.QUESTION_MESSA GE);
try {
// parseInt is a method of class Integer. parseInt converts a string into an integer value
val = Integer.parseInt(amount);
if (val < 0){
JOptionPane.showMessageDia log(null, "Amount cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 2000000) {
JOptionPane.showMessageDia log(null, "Amount cannot exceed $2,000,000", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
rate = JOptionPane.showInputDialo g(null,"Pl ease enter finance rate:", " ", JoptionPane.QUESTION_MESSA GE);
try {
val = Integer.parseInt(rate);
if (val <0){
JOptionPane.showMessageDia log(null, "Finance rate cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 14) {
JOptionPane.showMessageDia log(null, "Finance Rate cannot exceed 14%", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
term = JOptionPane.showInputDialo g(null,"Pl ease enter term:", " ", JoptionPane.QUESTION_MESSA GE);
try {
val = Integer.parseInt(term);
if (val <0){
JOptionPane.showMessageDia log(null, "Term cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 480) {
JOptionPane.showMessageDia log(null, "Finance Rate cannot exceed 480 months", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
public static void main(String[] args) throws Exception
{
int val = 0;
String amount;
String rate;
String term;
int response = 0;
boolean valid = true;
do{
// The showInoutDialog is a method of JOptionPane and its return value is a string.
// Since the return value is a string, it is necessary that "grade" should be of a string
// datatype too.
amount = JOptionPane.showInputDialo
try {
// parseInt is a method of class Integer. parseInt converts a string into an integer value
val = Integer.parseInt(amount);
if (val < 0){
JOptionPane.showMessageDia
continue;
}
else if (val > 2000000) {
JOptionPane.showMessageDia
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia
continue;
}
rate = JOptionPane.showInputDialo
try {
val = Integer.parseInt(rate);
if (val <0){
JOptionPane.showMessageDia
continue;
}
else if (val > 14) {
JOptionPane.showMessageDia
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia
continue;
}
term = JOptionPane.showInputDialo
try {
val = Integer.parseInt(term);
if (val <0){
JOptionPane.showMessageDia
continue;
}
else if (val > 480) {
JOptionPane.showMessageDia
continue;
}
catch(Exception e) {
// Error with the amount
JOptionPane.showMessageDia
continue;
}
ASKER
After each of the 3 catch sections? I added that and still get
'catch' without 'try'
catch(Exception e) {
^
MortgageGui1.java:24: 'try' without 'catch' or 'finally'
try {
^
'catch' without 'try'
catch(Exception e) {
^
MortgageGui1.java:24: 'try' without 'catch' or 'finally'
try {
^
Try with this code
public static void main(String[] args) throws Exception
{
int val = 0;
String amount;
String rate;
String term;
int response = 0;
boolean valid = true;
do{
// The showInoutDialog is a method of JOptionPane and its return value is a string.
// Since the return value is a string, it is necessary that "grade" should be of a string
// datatype too.
amount = JOptionPane.showInputDialo g(null,"Pl ease enter amount to be financed:", " ", JOptionPane.QUESTION_MESSA GE);
try
{
// parseInt is a method of class Integer. parseInt converts a string into an integer value
val = Integer.parseInt(amount);
if (val < 0)
{
JOptionPane.showMessageDia log(null, "Amount cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 2000000)
{
JOptionPane.showMessageDia log(null, "Amount cannot exceed $2,000,000", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
rate = JOptionPane.showInputDialo g(null,"Pl ease enter finance rate:", " ", JoptionPane.QUESTION_MESSA GE);
try
{
val = Integer.parseInt(rate);
if (val <0)
{
JOptionPane.showMessageDia log(null, "Finance rate cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 14)
{
JOptionPane.showMessageDia log(null, "Finance Rate cannot exceed 14%", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
term = JOptionPane.showInputDialo g(null,"Pl ease enter term:", " ", JoptionPane.QUESTION_MESSA GE);
try
{
val = Integer.parseInt(term);
if (val <0)
{
JOptionPane.showMessageDia log(null, "Term cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
else if (val > 480)
{
JOptionPane.showMessageDia log(null, "Finance Rate cannot exceed 480 months", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia log(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE) ;
continue;
}
public static void main(String[] args) throws Exception
{
int val = 0;
String amount;
String rate;
String term;
int response = 0;
boolean valid = true;
do{
// The showInoutDialog is a method of JOptionPane and its return value is a string.
// Since the return value is a string, it is necessary that "grade" should be of a string
// datatype too.
amount = JOptionPane.showInputDialo
try
{
// parseInt is a method of class Integer. parseInt converts a string into an integer value
val = Integer.parseInt(amount);
if (val < 0)
{
JOptionPane.showMessageDia
continue;
}
else if (val > 2000000)
{
JOptionPane.showMessageDia
continue;
}
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia
continue;
}
rate = JOptionPane.showInputDialo
try
{
val = Integer.parseInt(rate);
if (val <0)
{
JOptionPane.showMessageDia
continue;
}
else if (val > 14)
{
JOptionPane.showMessageDia
continue;
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia
continue;
}
term = JOptionPane.showInputDialo
try
{
val = Integer.parseInt(term);
if (val <0)
{
JOptionPane.showMessageDia
continue;
}
else if (val > 480)
{
JOptionPane.showMessageDia
continue;
}
}
catch(Exception e)
{
// Error with the amount
JOptionPane.showMessageDia
continue;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I added that and now that section is ok, no errors.
closing bracket } required after catch block