473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

caught in an infinite loop in my try and catch coding.

I can't figure out why, but I think that I am caught in an infinite loop, the program compiles, and runs, however when I enter a number that is out of the range, it doesn't throw an error message. here is the code that I think contains the improper coding, if you need the full code I have attached it on notepad.
Thanks
Expand|Select|Wrap|Line Numbers
  1.  public void actionPerformed(ActionEvent e)
  2.     {
  3.         //read user input into variables
  4.         int start = Integer.parseInt(startField.getText());
  5.         int stop = Integer.parseInt(stopField.getText());
  6.         int step = Integer.parseInt(stepField.getText());
  7.         for (int i=0; i<16; i++)
  8.         {
  9.              boardDisplay[i].setBackground(Color.magenta);
  10.              if(e.getActionCommand().equals("Clear"))
  11.                boardDisplay[i].setBackground(Color.white);
  12.              }
  13.         for (int i=start; i<=stop; i+=step)
  14.              {
  15.              boardDisplay[i].setBackground(Color.yellow);
  16.              if(e.getActionCommand().equals("Clear"))
  17.                boardDisplay[i].setBackground(Color.white);
  18.         }
  19.         boolean condition=true;
  20.         while (condition)
  21.  
  22.            {
  23.             try
  24.             {
  25.                 if (start < 0 | start > 16) throw new NumberFormatException();
  26.                 else condition = false;
  27.  
  28.             }
  29.             catch (NumberFormatException g)
  30.             {
  31.             System.out.println("Only enter 0 to 15 in number format.");
  32.             }
  33.  
  34.  
  35.                }
  36.         }
  37.  
  38.     public static void main(String[] args)
  39.  
  40.         {
  41.             Checkerboard f = new Checkerboard();
  42.             f.setBounds(50,100,300,400);
  43.             f.setTitle("Checkerboard Array");
  44.             f.setVisible(true);
  45.     } //end of main
  46. }
  47.  
Attached Files
File Type: txt Checkerboard.txt (3.1 KB, 377 views)
Mar 1 '11 #1
1 1978
Dheeraj Joshi
1,123 Expert 1GB
There are couple of problems in the code.
You have declared an array of length 16
Expand|Select|Wrap|Line Numbers
  1. TextArea boardDisplay[] = new TextArea[16];
  2.  
But in actionPerformed method. You try to fill these arrays.
Expand|Select|Wrap|Line Numbers
  1. for (int i=start; i<=stop; i+=step)
  2. {
  3.          boardDisplay[i].setBackground(Color.yellow);
  4.          if(e.getActionCommand().equals("Clear"))
  5.          boardDisplay[i].setBackground(Color.white);
  6. }
  7.  
Suppose start is 5 and stop is 55 and step is 5
your code tries to fill following items in array are changed with colour
Expand|Select|Wrap|Line Numbers
  1. boardDisplay[5]
  2. boardDisplay[10]
  3. boardDisplay[15]
These are legitimate entries. But now your for loop is not completed, since
Expand|Select|Wrap|Line Numbers
  1. i<=stop true (15 <= 55)
So for loop tries to run again increments i to 20 and tries to fill
Expand|Select|Wrap|Line Numbers
  1. boardDisplayp[20]
. This is out of bound (Remember your array is from 0-15 there is no 20th element and it throws array index out of bound exception.)

Solution is check the start stop values before executing these loops. And if they are out of range throw an error message in GUI and exit.

You can rewite like
Expand|Select|Wrap|Line Numbers
  1. int start = Integer.parseInt(startField.getText());
  2. int stop = Integer.parseInt(stopField.getText());
  3. int step = Integer.parseInt(stepField.getText());
  4.  
  5. try{
  6. if ((start < 0 || start >= 16) || (stop<start) || (stop >= 16 || stop < 0)) throw new NumberFormatException();
  7. else condition = false;
  8. }
  9. catch(Exception ex){
  10.         System.out.println("Exception");
  11. }
  12.  
Regards
Dheeraj Joshi
Mar 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
0
by: LowRyder | last post by:
I haven't been able to find any fixes for this. I have a form with two memo fields. Sometimes when you push the spell check button or press F7 the spell check goes into an infinite loop. If you...
2
by: yawnmoth | last post by:
I've been expirementing with CSS expressions and have encountered a problem that doesn't make a lot of sense to me. After playing around with it, I've managed to reduce it to the following: <a...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
5
by: mc | last post by:
I have a function (shown below) that I call to check if a server is present. when a server is present, everything is ok. however when it fails. I get an SocketException fired. On my development...
0
by: William Johnston | last post by:
Hi, A support technician proposed a workaround to black images created for thumbnails. The code to create a thumbnail is below: //passes results to Response.OutputStream private void...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.