exception error help

Discussion in 'Java' started by taurus, Dec 10, 2006.

  1. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Try using

    Code:
    try
    {
      //
    }
    catch(e)
    {
      //
    }
    finally
    {
      // your code here
    }
    
     
  3. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    so wat does the finally do. it will fix ma problem?
     
  4. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice