Results 1 to 2 of 2
  1. #1
    janey4115 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Logic Error using loop and array

    Hi - the below code is attempting to loop through an array and print out any matching words if no matching words are found a message should be printed advising the user of this, however when no matching words are the message to the user is printing 6 times due to the for loop and when matching words are found the message is still printing once. I'm unsure how to change the below to get this working as required?

    Word [] ref = new Word [2];
    ref[0] = new Word ("sad")
    ref[1] = new Word ("happy")

    String word;

    System.out.print("Please enter word: ");

    word = scanner.nextLine();

    for (int j = 0; j<2 ; j++)

    {

    if (ref[j].getword().equalsIgnoreCase
    (word))

    {


    System.out.printf("Word: = %-2s " , ref[j].getword()+ " ");

    }
    else
    System.out.println("There are no matching words");
    }

  2. #2
    Tien is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Logic Error using loop and array

    I do not really understand the purpose of your program. (Also put some points and commas in your sentences, it makes it easier to read.) And how can it print 6 times if you only have a loop of 2 runs and 2 printing options? Is this the entire code?

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  2. Array or for loop? or both?
    By kedecr in forum New To Java
    Replies: 4
    Last Post: 03-09-2011, 02:24 PM
  3. help with a logic error
    By ShinTec in forum Advanced Java
    Replies: 11
    Last Post: 05-02-2010, 11:19 PM
  4. Logic Error: simulated accuracy
    By Kaito in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 02:39 PM
  5. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-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
  •