Index of List

S

Subhabrata

Dear Group,

I am a new programmer in Java.

I want to have check whether one word from one list is there in
another list and if it is
there I like to extract its next word.

For example,
If I have a string like,

"Moscow is the capital of Russia"

The wordlist would be "Moscow", "is","the","capital","of","Russia"

Now suppose I have a string of words like,

"Moscow", "Leningrad", "is", "was", "the", "a", "capital", "state",
"of", "by", "Russia", "Romania"

Here as one word is identified from the first string in the second
string the cursor points to the next word, rather it exploits hashing.

I know:
(i) Declaring String.
(ii)Using String.split
(iii) I can use the for loop over the string and find out indexing.
(iv) I know .equals

But I think I am missing something like if Java has .find or .index
sort of thing then it would be very easy to do.

Does Java have anything?

Regards,
Subhabrata.
 
T

Tim Slattery

But I think I am missing something like if Java has .find or .index
sort of thing then it would be very easy to do.

There is an indexOf method in the String object.
 
J

John B. Matthews

<[email protected]
m>,
Subhabrata said:
I am a new programmer in Java.

I want to have check whether one word from one list is there
in another list and if it is there I like to extract its next
word.

For example, If I have a string like,

"Moscow is the capital of Russia"

The wordlist would be "Moscow", "is","the","capital","of","Russia"

Now suppose I have a string of words like,

"Moscow", "Leningrad", "is", "was", "the", "a", "capital",
"state", "of", "by", "Russia", "Romania"

Here as one word is identified from the first string in the
second string the cursor points to the next word, rather it
exploits hashing.

I know:
(i) Declaring String.
(ii)Using String.split
(iii) I can use the for loop over the string and find out indexing.
(iv) I know .equals

But I think I am missing something like if Java has .find or .index
sort of thing then it would be very easy to do.

Does Java have anything?

Implementations of the java.util.List interface have a
convenient contains() method, Collections.binarySearch() may be
warranted for searching sufficiently long lists.

Implementations of the java.util.Map interface map keys to
values, often using hashing.

As you are new to Java, it might be worth starting here:

<http://docs.oracle.com/javase/tutorial/collections/index.html>
 
A

Arne Vajhøj

I want to have check whether one word from one list is there in
another list and if it is
there I like to extract its next word.

For example,
If I have a string like,

"Moscow is the capital of Russia"

The wordlist would be "Moscow", "is","the","capital","of","Russia"

Now suppose I have a string of words like,

"Moscow", "Leningrad", "is", "was", "the", "a", "capital", "state",
"of", "by", "Russia", "Romania"

Here as one word is identified from the first string in the second
string the cursor points to the next word, rather it exploits hashing.

I know:
(i) Declaring String.
(ii)Using String.split
(iii) I can use the for loop over the string and find out indexing.
(iv) I know .equals

But I think I am missing something like if Java has .find or .index
sort of thing then it would be very easy to do.

Does Java have anything?

For inspiration:

import java.util.ArrayList;
import java.util.List;

public class Bad {
private static List<String> wordlist = new ArrayList<String>();
static {
wordlist.add("Moscow");
wordlist.add("Leningrad");
wordlist.add("is");
wordlist.add("was");
wordlist.add("the");
wordlist.add("a");
wordlist.add("capital");
wordlist.add("state");
wordlist.add("of");
wordlist.add("by");
wordlist.add("Russia");
wordlist.add("Romania");
}
public static void test(String s) {
for(String w : s.split("\\W")) {
int ix = wordlist.indexOf(w);
if(ix >= 0) {
System.out.println(w + " -> " + wordlist.get(ix + 1));
} else {
System.out.println(w);
}
}
}
public static void main(String[] args) {
test("Moscow is the capital of Russia X");
}
}

and

import java.util.HashMap;
import java.util.Map;

public class Good {
private static Map<String, String> translation = new HashMap<String,
String>();
static {
translation.put("Moscow", "Leningrad");
translation.put("is", "was");
translation.put("the", "a");
translation.put("capital", "state");
translation.put("of", "by");
translation.put("Russia", "Romania");
}
public static void test(String s) {
for(String w : s.split("\\W")) {
String w2 = translation.get(w);
if(w2 != null) {
System.out.println(w + " -> " + w2);
} else {
System.out.println(w);
}
}
}
public static void main(String[] args) {
test("Moscow is the capital of Russia X");
}
}

Arne
 
R

Roedy Green

I am a new programmer in Java.

HashSet will quickly tell you if a word is in a list, but it won't
give you any order.

You can export the data to a array or ArrayList and sort it. Then you
can linearly scan for it or use binary search.

Or you can build a HashMap that gives you the order# given the word.

This all blows up in you face if you keep adding words,

see
http://mindprod.com/jgloss/hashset.html
http://mindprod.com/jgloss/hashmap.html
http://mindprod.com/jgloss/arraylist.html
http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/binarysearch.html
http://mindprod.com/jgloss/sort.html

Your question may be ill-formed. It seems to me there can be multiple
answers to the question.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top