Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: loops and exception handling

  1. #1
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default loops and exception handling

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    
    
    public class Array extends JFrame{
        
        private JTextField number = new JTextField(10);
        private JLabel result;
        int num;
        ArrayList<Integer> list = new ArrayList<>();
        
        
        public static void main(String[] args){
            
            Array frame = new Array();
            frame.setTitle("Collections");
            frame.setSize(700, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            
            
        }
        
        public Array() {
        
           
            add(new JLabel("You will enter 5 integers."), BorderLayout.NORTH);
            setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
            add(new JLabel("Enter a number and click the enter button:"));
            add(number);
            
            JButton jbtEnter = new JButton("Enter");
            jbtEnter.setToolTipText("This will store your number in the program");
            add(jbtEnter);
            
            jbtEnter.addActionListener(new EnterListener());
            
            
            JButton jbtDisplay = new JButton("Display");
            jbtDisplay.setToolTipText("This will display all the integers in numberical order");
            add(jbtDisplay);
            
            jbtDisplay.addActionListener(new DisplayListener());
            
            result = (new JLabel(""));
            add (result);
    }
        class EnterListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e){
                
                
                try{
                int i = 0;  
                if (i < 5){
                    
                    
                num = Integer.parseInt(number.getText());
                list.add(num);
                number.setText(null);
                i++;
                
                } 
                catch (IndexOutOfBoundsException ex){
                    JOptionPane.showMessageDialog(null, "Error. You cannot exceed the limit of 5 entries.  click other button.", "Error", JOptionPane.ERROR_MESSAGE);
                }
                
             }
        }
             
        
        class DisplayListener implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent e) {
                
                
                Collections.sort(list);
                result.setText("The numbers you have entered, listed in numerical order is: " + list );
                }
                
        }
    
    }
    I am having problems with the loop and exception handling in the EnterListener. Any Advice?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: loops and exception handling

    I am having problems
    Please explain what the problem is. Where is the loop?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: loops and exception handling

    line 52 - 73. I have tried multiple items in this section to make the user have to stop entering numbers after the user entered 5 numbers. Every time I run the program, the program allows the user to keep entering numbers.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: loops and exception handling

    Can you explain how you think the loop in your code works?
    Lines 52-73 define the whole of a class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: loops and exception handling

    What I have been trying to make it do is if the user enters the 6th number it will be thrown to the catch and tell the user that there cannot be more than 5 entries. then the user would have to select the display button to see all of the entries in numerical order.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: loops and exception handling

    Why throw an exception? Use an if to detect the 6th number and show the message there.

    The code sets i to 0 before testing it in an if statement. i will always be 0 in the if test. i needs to be defined outside of a method so it will retain its value from one call to the method to the next call.

    Where is the loop?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: loops and exception handling

    figured it out. thank you. the i should have been defined outside the method. thanks for the help

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: loops and exception handling

    figured it out
    It would be polite to let javaprogrammingforums.com know.

    Have a read of Be Forthright When Cross Posting To Other Sites at Java Ranch: advice which applies to most sites.
    Fubarable likes this.

Similar Threads

  1. Exception handling
    By Kartiky14 in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 08:07 AM
  2. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM
  3. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •