Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   exception error help (http://www.go4expert.com/forums/exception-error-help-t2192/)

taurus 11Dec2006 01:25

exception error help
 
i am trying to use trys and catches (exception handling), but it doesnt seem to work correctly. I got the following code:
Code:

import javax.swing.*;
public class brand
{
    public static void main (String[] args)
    {
        String amount, input;
        int number=0, total, again;
        do
        {
            amount = JOptionPane.showInputDialog(null, "How many numbers do you want to enter");
            try
            {
                number = Integer.parseInt(amount);
            }
            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Error: You did not enter a number");
            }
            int[] array = new int[number];
            for(int index=0; index < array.length; index++)
            {
                input = JOptionPane.showInputDialog(null, "Enter a positive integer number" + (index +1));
                try
                {
                    array[index] = Integer.parseInt(input);
                    while(isPositive(array[index]) == false)
                    {
                        array[index] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a positive integer number" + (index +1)));
                    }
                }
                catch(NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "Error: You did not enter a positive integer number");
                }
            }
            for(int index=0; index<array.length; index++)
            {
                total = addNumbers(array[index]);
                JOptionPane.showMessageDialog(null, "The total of integers between 1 and " + array[index] + " is " + total);
            }
            again = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
        }
        while(again == JOptionPane.YES_OPTION);
    }
    public static boolean isPositive(int number)
    {
        if(number>0)
        return true;
        else
        return false;
    }
   
    public static int addNumbers(int number)
    {
        int total=0;
        int count=0;
        while(count<number)
        {
            count = count + 1;
            total = total + count;
        }
        return total;
    }
}

Now the try and catches dont work. they catch the error yes but they dont like go back so i can repeat my entry. They just go to the next event to be done in ma program.
What am i doin wrong and what should i do to change it??
THANKS

pradeep 11Dec2006 10:48

Re: exception error help
 
Try using

Code: Java

try
{
  //
}
catch(e)
{
  //
}
finally
{
  // your code here
}


taurus 11Dec2006 13:52

Re: exception error help
 
so wat does the finally do. it will fix ma problem?

taurus 11Dec2006 22:08

Re: exception error help
 
wait ok i think i have solved it. i used some while boolean valid thing. seems to work.
Is there anyway i can handle negative numbers as errors. cos say i dont want the user to be able to enter a negative number. An error message should appear. Is there anyway??
THANKS


All times are GMT +5.5. The time now is 17:48.