This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

How to match a string name into an array value?  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys,
i have a problem.

i have a class which i created an City array which contains the City name,  number of residents, number of neighborhoods.
in the City class i have a method calls : getCityName();
now, i have a class called Country.
in the Country class i have private variables of City[] cities - which are an array of Cities.
I need to create a method that will  get a city name from the user, and will print to the user, whether the name he entered  has a match to one of the city names in the array or not.
i tried to do so but it doesn't work for me .
every output is saying ""There was no match inside the citise array to the name " +cityName.. even if i put the same city names
please help me

 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cause you're searching for the city in an empty array of cities.
You don't initialize the values of the array.
Note, this is the 4th or 5th question you ask about the same subject, but after getting some response you never let us know if you got it working, a little feedback or thanks on the responses you get would be nice...
 
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this:
Go through it step by step.  Keep in mind what tempName will be.  What happens if cityName isn't the last element in your array?
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:cause you're searching for the city in an empty array of cities


Ha!  That too, huh?
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:cause you're searching for the city in an empty array of cities.
You don't initialize the values of the array.
Note, this is the 4th or 5th question you ask about the same subject, but after getting some response you never let us know if you got it working, a little feedback or thanks on the responses you get would be nice...



I do initialize the values of the cities array in the loop in the main method

and about the feedback you are right, usually when i don't answer it means you helped me and i don't need to ask anything else but you are  right this is not kind and from now on i will answer on your answers
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute is still right...
You have to break out of the loop once you found the right value...
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Take a look at this:
Go through it step by step.  Keep in mind what tempName will be.  What happens if cityName isn't the last element in your array?



cityName isn't need to be a value inside the array, the cityName is a name that the user input and then by the for loop , it search for a match between the tempName and the user's input name.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a lot of probs reading you code, I believe C.R already gave you the advice once to put the code to initialize your array in your constructor...
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:No you don't, the class you thin you initialize is test and the class that returns the array is tester, even if you would Knute is still right...
You have to break out of the loop once you found the right value...



But test is an object of the Testerr class, so i initialize the test class because only there i can initialize the array values , and about the loop and the value, inside the loop i said :
if the array of the index "j" . get the name of the object is  equals to the given input from the user.
and you are right, i put a break after the "if " statement and its command below, but still. i get the output of "There was no match inside the citise array to the name "+cityName"
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you show me the way  i will get the output of the "cityName" and not the output saying "There was no match inside the citise array to the name "+cityName;"
i'm so lost
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here test is a local variable because it's declared within the main method.  It goes out of scope when you call the returnMatch() method.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I was you I would have a look at some tutorials on how to write clean code, it would greatly aprove the readabillity of your code and not only help others that trying to help you understand your code better, but also yourself when you getting back to it after a long time.

can you show me the way  i will get the output of the "cityName" and not the output saying "There was no match inside the citise array to the name "+cityName;"
i'm so lost


simply at break; to your if statement
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:can you show me the way  i will get the output of the "cityName" and not the output saying "There was no match inside the citise array to the name "+cityName;"
i'm so lost


Well, I'm not sure why you would need the variable test at all.  Why not use cities?  
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:

rian bron wrote:can you show me the way  i will get the output of the "cityName" and not the output saying "There was no match inside the citise array to the name "+cityName;"
i'm so lost


Well, I'm not sure why you would need the variable test at all.  Why not use cities?  





Still won't work
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If I was you I would have a look at some tutorials on how to write clean code, it would greatly aprove the readabillity of your code and not only help others that trying to help you understand your code better, but also yourself when you getting back to it after a long time.



You can also read the Wiki entry for HowToFormatCode (that's a link).

can you show me the way  i will get the output of the "cityName" and not the output saying "There was no match inside the citise array to the name "+cityName;"
i'm so lost


simply at break; to your if statement


And use a variable that's in scope for both the main() method and the returnMatch() method.
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this closely:
What are you comparing?  A City or a cityName?
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Look at this closely:
What are you comparing?  A City or a cityName?


i want to compare the cityName , thats why i need to wriite  
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't really tried it, but you could do something like this...




We don't know what you're trying to do in your other classes, so this will not be a copy paste solution (which isn't the purpose anyway), but it should give you at least an idea of how to tackle your prob
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:i want to compare the cityName , thats why i need to wriite  


Correct.  And you don't need the getCities() method, just cities[j].getCityName().equals(cityName).
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!