Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #21706 > unrolled thread
| Started by | Subhabrata <subhabangalore@gmail.com> |
|---|---|
| First post | 2013-01-25 09:07 -0800 |
| Last post | 2013-01-26 19:22 -0800 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Index of List Subhabrata <subhabangalore@gmail.com> - 2013-01-25 09:07 -0800
Re: Index of List Tim Slattery <Slattery_T@bls.gov> - 2013-01-25 12:38 -0500
Re: Index of List "John B. Matthews" <nospam@nospam.invalid> - 2013-01-25 12:48 -0500
Re: Index of List Arne Vajhøj <arne@vajhoej.dk> - 2013-01-25 21:44 -0500
Re: Index of List Roedy Green <see_website@mindprod.com.invalid> - 2013-01-26 19:22 -0800
| From | Subhabrata <subhabangalore@gmail.com> |
|---|---|
| Date | 2013-01-25 09:07 -0800 |
| Subject | Index of List |
| Message-ID | <8fb20d2d-260b-42a1-9899-d038d2abfa5e@gg5g2000pbc.googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Tim Slattery <Slattery_T@bls.gov> |
|---|---|
| Date | 2013-01-25 12:38 -0500 |
| Message-ID | <mng5g8hbj4snvh3o1fajvt9ec82b0vnmcr@4ax.com> |
| In reply to | #21706 |
Subhabrata <subhabangalore@gmail.com> wrote: >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. -- Tim Slattery Slattery_T@bls.gov
[toc] | [prev] | [next] | [standalone]
| From | "John B. Matthews" <nospam@nospam.invalid> |
|---|---|
| Date | 2013-01-25 12:48 -0500 |
| Message-ID | <nospam-02736B.12483425012013@news.aioe.org> |
| In reply to | #21706 |
In article <8fb20d2d-260b-42a1-9899-d038d2abfa5e@gg5g2000pbc.googlegroups.co m>, Subhabrata <subhabangalore@gmail.com> wrote: >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> -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-01-25 21:44 -0500 |
| Message-ID | <51034314$0$290$14726298@news.sunsite.dk> |
| In reply to | #21706 |
On 1/25/2013 12:07 PM, Subhabrata wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-01-26 19:22 -0800 |
| Message-ID | <3r69g8ljnsoimgtc8srgutra3qa08nmejr@4ax.com> |
| In reply to | #21706 |
On Fri, 25 Jan 2013 09:07:33 -0800 (PST), Subhabrata <subhabangalore@gmail.com> wrote, quoted or indirectly quoted someone who said : > >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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web