Results 1 to 8 of 8
Thread: loops and exception handling
- 05-06-2012, 02:32 AM #1
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
loops and exception handling
I am having problems with the loop and exception handling in the EnterListener. Any Advice?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 ); } } }
- 05-06-2012, 02:42 AM #2
Re: loops and exception handling
Please explain what the problem is. Where is the loop?I am having problemsIf you don't understand my response, don't ignore it, ask a question.
- 05-06-2012, 02:48 AM #3
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
- 05-06-2012, 02:53 AM #4
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.
- 05-06-2012, 02:57 AM #5
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
- 05-06-2012, 03:12 AM #6
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.
- 05-06-2012, 03:23 AM #7
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
Re: loops and exception handling
figured it out. thank you. the i should have been defined outside the method. thanks for the help
- 05-06-2012, 03:46 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: loops and exception handling
It would be polite to let javaprogrammingforums.com know.figured it out
Have a read of Be Forthright When Cross Posting To Other Sites at Java Ranch: advice which applies to most sites.
Similar Threads
-
Exception handling
By Kartiky14 in forum New To JavaReplies: 3Last Post: 03-25-2012, 08:07 AM -
Exception handling
By herat in forum New To JavaReplies: 1Last Post: 06-21-2011, 10:13 AM -
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks