• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Why do the boolean for loop skip the else statement?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I´m working on an array that shall compare user input with what´s already in array. If userinput actFNamn is already in array it won´t be added again, and if not in array add to array. I tried different combinations but eventually decided to use a boolean so I could have a for loop iterating the array. The for loop is at line 4-14 no issues there it loops trought array if in array it prints out line 19. In my mind if I as a user input something that is not in array it would 1. loop array it would not find it and 2. the boolean would be considered false so 3. it would go to the else at line 21 and print out line 24. It´s obviously does not it jumps over the else and do nothing at line 39.

But how would I go about making the boolean false and do the else line at 21?


When I do add the contains = false anywhere in the for loop it would of course print out the else no matter what which defeats the purpose of making sure there is no duplicates in the array in the first place. Now this is a separate question but the code in the else at line 21-36 when alone succeds in adding to array at the next null value but in my tests in loops it does not but instead overwrite existing array [0] (there is already some objects in the array), why does it not work the same outside as inside the loop?


Java-Examples (Array contains)


 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unfortunately an array cannot compare input to its contents. You need outside code to do it, and I am afraid you haven't found a very good resource. The code formatting in that link is poor. I am pleased to see corrected some of the formatting, but the comments in that link aren't useful. For example that on their line 32 says,

//check if string array contains the string

which is incorrect. I presume you need to use array because there are more sophisticated data structures which actually do check whether they contain a particular element.
You appear to be implementing a linear search, which is quite a simple algorithm. Note that a search usually returns the index at which the element is found or −1 is it isn't found. You can test whether the loop needs to run by testing for contains or the value of the index.I presume your else isn't reached because you always find the object you are seeking. You may be able to shorten the code to print results with the ?: operator.
You are right to test for nulls (your line 7) but as long as your input isn't null, you can reverse the objects for the equals() test and not worry about nulls.
I am not happy about adding a new object in the same else. I haven't got the time to say anything else now: sorry.
 
Campbell Ritchie
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An hour ago, I wrote:. . .. . .

...and I made a mistake in line 9: it should readThe original code wouldn't compile.
Line 4 tests whether the element at that array index if the same object as your object sought.
Are you resetting contains to false before entering the loop again?
If your array doesn't contain a particular film object, I would sooner have separate code add it to the array. Be careful about counters; it is possible to overstep the bounds of the array if you don't check if (counter < myArray.length) ... It is also possible mistakenly to overwrite existing objects in the array..
 
Campbell Ritchie
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you counting the number of elements in the array? You would want your counter variable to reflect the number of elements in the array. In which case it should be a field. If it is a local variable, it probably won't count the elements correctly.
 
And inside of my fortune cookie was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic