Results 1 to 2 of 2
Thread: Logic Error using loop and array
- 11-17-2011, 02:18 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
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");
}
- 11-17-2011, 06:24 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Array or for loop? or both?
By kedecr in forum New To JavaReplies: 4Last Post: 03-09-2011, 01:24 PM -
help with a logic error
By ShinTec in forum Advanced JavaReplies: 11Last Post: 05-02-2010, 10:19 PM -
Logic Error: simulated accuracy
By Kaito in forum New To JavaReplies: 3Last Post: 10-27-2009, 01:39 PM -
Logic Error: Not Writing To File
By JDCAce in forum Advanced JavaReplies: 6Last Post: 10-21-2008, 02:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks