• Post Reply Bookmark Topic Watch Topic
  • New Topic

Matching Up Items From One Array With A Text File  RSS feed

 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I've had a great experience with the Ranch so far and could use some more help.
I have a mapping text file that looks something like this:


I need to create a function that finds all the mappings for a given array of IDs.
So if I gave it the array ['1.66.12.16.17', '10.155.12'], it should give me back [oneMore, myItem1].
Perhaps pseudoCode would help (with my thoughts)


That's just my thought process, but it is clearly not in the proper syntax and not very efficient... This is where I can use your help. I come from a scripting language, so it's a little difficult for me to get used to Java. Any help is appreciated!
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I will suggest you to use java.util.Properties to read and load the mapping in a text file. If you are new to this then you can look at below example. Lets also wait and hear from others.
Keep the below in a property file - say sample.properties
myItem1 100.122.15.16
anotherItem 10.155.12
oneMore 1.66.12.16.17

You can read the properties you have mentioned using java.util.Properties like below.
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again John Jai! Unfortunately, I have no control over the file... It will be provided to me via .txt or some other random format. Is there a way to create our own HashTable splitting each line on a ' ' and then using similar code to what you have? I'm sorry, I'm very new to Java and seeing these examples will greatly help me learn.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I misled you. The ".properties" is just a conventional usage for property files. You can use even "sample.txt". Properties class is using only the FileInputStream in the given sample.
Please look at the load() methods in Properties java doc for more information.
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now, thank you!

I've tried converting it into what I need, but I'm having trouble with the String Array. I can't add items to a String Array, but I can't seem to convert the ArrayList back to a String Array... How can I get around this? Thanks much for your help...

Here's my failed attempt at the code:
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer this to fix the Exception you receive - old ranch thread
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things - iterator should be placed inside the for loop like below

Second is that specify size while casting Object[] to String[]
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John Jai, I tried changing it to


But I'm still getting the same error. What am I doing wrong?
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the updated method based on your two suggestions. Your help is really appreciated! I have no idea why it's still returning that error.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you receiving compilation error or runtime error.
If the error is "This method must return a result of String[]" then please put the return variableArray statement after the catch{} block and see.
If still you have the error post it
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm seeing it in Eclipse before running it.
That was something I tried as well, but the variable was out of scope. Maybe I should define it as empty outside the try statement?

My modifications seemed to work, but I don't like how it's coded... Is there any other way of working with variable size String Arrays? John, thanks so much for your continued help...

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not very comfortable with Arrays & Collections.... lets see if others reply with better solutions
Maybe I should define it as empty outside the try statement?
- yes
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thank you very much - you've been a big part in helping me get started with Java.

Can you explain a little more about what each of these lines are doing?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An iterator is used to iterate over the objects present in a collection. Here we iterate through the Set.
Iterator documentation
 
KayCee Clark
Greenhorn
Posts: 13
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, this looks like a directed graph problem.

I often use a hashtable for the string keys ( from nodes ) with the values being references to bags ( multisets ). The bag holds all the "to" nodes. ( In your case, the numeric values. )

I would place the numeric values ( as strings ) in the bag for the matching key. Then reverse the graph. Now you have a graph where the numeric strings are among the keys, and you can just look in the bag for a particular value in your array to see what matches are there.

Graphs are a very powerful tool to have in your toolbox. You can find them in most algorithm books. Some sample java code online can be found at http://algs4.cs.princeton.edu/40graphs/.
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:An iterator is used to iterate over the objects present in a collection. Here we iterate through the Set.
Iterator documentation


Thanks, how does the Java code know that the separator for the key and value is a space? Is that a default value?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!