473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Searching an array, help!

Hi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!
Mar 2 '08 #1
5 1221
sukatoa
539 512MB
Hi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!

If you mean that you are using hashmap,

for example; HashMap<String, Color>....

You may want to use String as your key.... to find the color...

If i am right, then you can use for loop of it...
then try to get the Color from that hashmap...
As long as you know the String, use can implement an iteration that would compare them by all of you strings....just to get the needed color...

like,

Expand|Select|Wrap|Line Numbers
  1.  Color c = HashMapObject.get(String);
You can use the HashMapObject to get all the string and use it in loop...

example,

Set<String> key = HashMapObject.keySet();
It returns an array of String (Depends on what you have stored in you
HashMap) that is useful for iteration....

for( Initialize a String here: key)
it will iterate until the last String that you have stored in your
HashMap... then find the color...

If you feel it is too far from my assumptions,
Please be specific,
trying to seach an array
for me is confusing...


Correct me if im wrong,
Sukatoa
Mar 2 '08 #2
BigDaddyLH
1,216 Expert 1GB
Hi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!
Post a short example program of what you are trying to do.
Mar 3 '08 #3
<code>
public static String destSearch(String searchDest){

for(int j=0; j<times.arrive.length;++j){

if(where[j].equalsIgnoreCase(searchDest)){

Scanner inn=new Scanner(System.in);
System.out.println("Enter the time you want to get on");
Double requested_time = inn.nextDouble();
System.out.flush();

for(int i=j; i<times.depart.length;++i){

if(depart[i]==(requested_time)){

return times.depart[i] +times.where[i] +times.arrive[i];
}

else {
return times.depart[j] +times.where[j] +times.arrive[j];
}
}


}
}return "Not found";
}
</code>
this is the problematic code. i have three arrays in which journey details are stored: the positions of the data in them matches up, so if the place i wanted to go to was at position 1 in the "where" arrray, the times it left and arrived would be at the same positions in the other arrays.

i am aiming to make the above code look for the place they want to go, and when it finds that look through the "depart" array in order to find the time that they want to go. the problem at the moment is that it stops after it finds the first occurence of a place, whereas it should continue! any ideas would be welcomed!
Mar 3 '08 #4
r035198x
13,262 8TB
Use OOP instead.
Create a Journey class with the appropriate fields.
Then you only need to store one ArrayList<Journey>.,
Arrays are so un-OOP.
Mar 3 '08 #5
BigDaddyLH
1,216 Expert 1GB
Use OOP instead.
Create a Journey class with the appropriate fields.
Then you only need to store one ArrayList<Journey>.,
Arrays are so un-OOP.
Indeed. Parallel arrays like this:

Expand|Select|Wrap|Line Numbers
  1. times.depart[i] +times.where[i] +times.arrive[i]
Are a symptom of the code smell "object denial".
Mar 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
1
by: romain.jouin | last post by:
Hi everyone, I just wanted to propose this piece of code which is searching recursively a value in an array... and let you indicate me if there is an error... function...
6
by: Bernie Yaeger | last post by:
I need a little help with an algorithm. Let's say I have an array with 15 items. I want to find "Fern" where there are "Able", "Me Also", "Zimmerman", etc in no particular order. Forget about...
2
by: Carlos K | last post by:
Hello I'm having some difficulty searching a set of rows in a DataRow collection.. This is my question What would be an efficient way to search any column of an DataRow array Let me try to...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.