Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with an array in a timer

  1. #1
    Junior Member
    Join Date
    Oct 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Problem with an array in a timer

    Hi, I have a code, which includes arrays. I i'm not an expert but as far as I know, it's supposed to work. The code is supposed to delete any non-integer characters from the textfields in the array. Unfortunately it doesn't, but i can't figure out why.
    This is the array:

    private JTextField[] textFields = {
    txtv1, txtv2, txtv3, txtv4, txtv5, txtv6, txtv7, txtv8, txtv9, txtv10,
    txtv11, txtv12, txtv13, txtv14, txtv15, txtv16, txtv17, txtv18, txtv19,
    txtv20, txtv21, txtv22, txtv23, txtv24, txtv25, txtv26, txtv27, txtv28,
    txtv29, txtv30, txtv31, txtv32, txtv33, txtv34, txtv35, txtv36,
    txtv1to12, txtv13to24, txtv25to36, txtv3to36, txtv2to35, txtv1to34,
    txtv00, txtv0, txtv1to18, txtveven, txtvzwart, txtvodd, txtv19to36, txtvrood
    };

    This is the timer:

    final Timer t3 = new Timer(5000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    for (int t = 0; t < 51; t++) {
    if (!(textFields[t]==null)) {
    try {
    int x = Integer.parseInt(textFields[t].getText());
    } catch (Exception ex) {
    textFields[t].setText(textFields[t].getText().substring(0, textFields[t].getText().length() - 1));
    }
    }
    }

    }
    });
    t3.start();
    Can someone please tell me what's wrong with this timer?
    Last edited by Tobatobatoba; October 19th, 2023 at 04:24 AM.

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    43
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Problem with an array in a timer

    It seems like the issue with your timer lies in the loop where you're attempting to parse the text from the text fields into integers. Here are a few things to consider:

    1. Array Bounds: Your loop condition `t < 51` suggests you're iterating over 51 elements, but your array only has 50 elements (indices 0 to 49). This may lead to an `ArrayIndexOutOfBoundsException` when accessing `textFields[50]`. You should adjust the loop condition to `t < textFields.length`.

    2. Null Check: Although you're checking if `textFields[t]` is not null before parsing, it seems like your array `textFields` may contain null elements. Ensure that all elements in the `textFields` array are initialized before using them in the loop.

    3. Substring Error: When catching an exception in the `catch` block, you're attempting to trim the text field content by one character using `substring(0, textFields[t].getText().length() - 1)`. This might not be the best approach because it's just removing the last character without checking whether it's non-integer or not. Consider implementing a more robust method to filter out non-integer characters.

    Here's the modified version of your timer code considering these points:

    ```java
    final Timer t3 = new Timer(5000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    for (int t = 0; t < textFields.length; t++) {
    if (textFields[t] != null) {
    try {
    // Attempt to parse integer
    int x = Integer.parseInt(textFields[t].getText());
    } catch (NumberFormatException ex) {
    // Non-integer detected, handle accordingly
    String originalText = textFields[t].getText();
    // Remove non-integer characters
    String filteredText = originalText.replaceAll("[^0-9]", "");
    // Set the text field with filtered content
    textFields[t].setText(filteredText);
    }
    }
    }
    }
    });
    t3.start();
    ```

    This code iterates over all elements in the `textFields` array, checks for nullity, attempts to parse integer, and handles the case where a non-integer is detected by removing non-integer characters from the text field. Make sure to replace `[0-9]` with your desired range of valid characters if needed.

    In addressing the non-integer character filtering issue, consider seeking additional resources or help with Java assignment. Exploring various programming forums or assignment help platforms like ProgrammingHomeworkHelp.com, where experts provide guidance on Java programming tasks could be beneficial. Additionally, consulting with online communities specializing in programming homework help might offer valuable insights into resolving such challenges effectively.

Similar Threads

  1. First timer here! Help with very very basic problem
    By brobertson300 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 31st, 2014, 01:44 PM
  2. Problem with timer
    By Hikaros in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2013, 12:30 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM
  5. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM

Tags for this Thread