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 9 of 9

Thread: Loop & custom exception error Please help

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Loop & custom exception error Please help

    Below is a method i am using which contains a for loop. it loops through the words in the array and if the input doesnt match one of the strings in the array, it throws an exception, well thats the idea anyway. But it is throwing the exception when it gets a match, as well as not getting a match... Any help would be greatly appreciated


    public static void englishWordExist(String input) throws WordDoesntExistException
    {

    for (int x=0; x<englishWords.length; x++)
    {
    if (input.equals(englishWords[x]))
    {
    Assignment2GUI.outputLabel.setText(irishWords[x]);
    }
    else
    {
    throw new WordDoesntExistException("This Word Is Not In The Dictionary");
    }
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,574
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Loop & custom exception error Please help

    But it is throwing the exception when it gets a match, as well as not getting a match.
    The posted code throws the exception on the first non-match.
    A better way would be to only throw the exception after all of the items in the list had been examined and no match had been found. Use a boolean variable to record that a match had been found and test that variable after the loop ends.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loop & custom exception error Please help

    I have tried that, and still it throws the error, im now loosing my patience

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,574
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Loop & custom exception error Please help

    I have tried that, and still it throws the error
    Post the code that shows what you did.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    dpjmie (April 18th, 2013)

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loop & custom exception error Please help

    public static void irishWordExist(String input) throws WordDoesntExistException
    {
    boolean error = false;
    for (int x=0; x<irishWords.length; x++)
    {
    if (input.equalsIgnoreCase(irishWords[x]))
    {
    Assignment2GUI.outputLabel.setText(englishWords[x]);
    }
    else
    {
    }

    if (error == true)
    {
    throw new WordDoesntExistException("This Word Is Not In The Dictionary");
    }
    }
    }

    --- Update ---

    i had The else as error = true in the above code;

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,574
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Loop & custom exception error Please help

    Have you solved the problem?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loop & custom exception error Please help

    Hi sorry about this, still not solved, here is the method with the suggested fix
    public static void irishWordExist(String input) throws WordDoesntExistException
    {
    boolean error = false;
    for (int x=0; x<irishWords.length; x++)
    {
    if (input.equalsIgnoreCase(irishWords[x]))
    {
    Assignment2GUI.outputLabel.setText(englishWords[x]);
    }
    else
    {
    error = true;
    }

    if (error == true)
    {
    throw new WordDoesntExistException("This Word Is Not In The Dictionary");
    }
    }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,574
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Loop & custom exception error Please help

    Your use of the error variable is reversed. You want to change its value when a match is found. A better name for the variable would be matchFound.
    Then at the end of the loop the code can test its value to see if a match was found and throw the exception if no match was found.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    dpjmie (April 18th, 2013)

  11. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loop & custom exception error Please help

    OMG, you are an absolute legend, thank you so much, i sincerely appreciate it

    Im such an idiot....

    Thanks again
    Dave

Similar Threads

  1. Null Exception Error & Help with PrintWriter
    By willo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 29th, 2011, 08:08 AM

Tags for this Thread