The Artima Developer Community
Sponsored Link

Java Answers Forum
for loop to read in every 10th word

6 replies on 1 page. Most recent reply: Nov 15, 2002 8:20 AM by ben

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

for loop to read in every 10th word Posted: Nov 13, 2002 4:24 AM
Reply to this message Reply
Advertisement
i hope someone can help
i have a hashtable called se and i want to be able to print out every 10th word that is inside the hashtable
any suggestions please
thanks ben


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 13, 2002 5:49 AM
Reply to this message Reply
Jdk 1.4 supports a LinkedHashMap that would make this easy for you. Otherwise, you'll have to convert your hashtable (which is archaic -- use a hashmap .. no overhead of syncronization) but you could just as easy put your object in an arraylist and then grab every tenth element.

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 13, 2002 5:55 AM
Reply to this message Reply
thanks daniel
but how would i put the hashtable into a array list can you give me some examples please or adjust my code please
thanks ben

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;

public class RT {


public static void main(String[] args) {
String cT = null;
String lT = null;
try {

//int ic = 1;
String Scounter = new String();
int icounter = 1;
Hashtable hash = new Hashtable(100);
FileReader file = new FileReader("Work.txt");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;
while (!eof){
s = new StringTokenizer(line.trim(),",",true);
while (s.hasMoreTokens()) {
cT = s.nextToken();
Scounter = String.valueOf(icounter);
if (cT.equals(",") && (lT != null) && lT.equals(",")){
hash.put(Scounter,"");

}
else {
if (!cT.equals(",")) {
hash.put(Scounter,cT);
}
}
lT = cT;
icounter++;
}
line = buff.readLine();
if (line == null)
eof = true;
}
for (Enumeration e = hash.elements();e.hasMoreElements();) {
String se = (String)e.nextElement();


System.out.println(se);



}
buff.close();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
}catch (NumberFormatException ne) {
System.out.println("Error - - " + ne.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());

}
}
}

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 14, 2002 11:32 AM
Reply to this message Reply
After you tokenize the string .. put the String object into an ArrayList;

while (s.hasMoreTokens()) {
cT = s.nextToken();
Scounter = String.valueOf(icounter);
if (cT.equals(",") && (lT != null) && lT.equals(",")){
hash.put(Scounter,"");

}

try something like

ArrayList list = new ArrayList();

String str;

while(s.hasMoreTokens()){
str = s.nextToken();

list.add(str);

}

int size = list.size();

for(int i=0; i<size; i++){
if((i % 10) == 0){
System.out.println(i);
}

I didn't test this code .. but give it a try and see what you get.

Ray

}

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 14, 2002 11:41 AM
Reply to this message Reply
If you have to use a hashmap .. maybe setting the key as an Integer and the value as a String might work.

For every token you have .. create a new Integer object using a counter. Put each of these in a hashmap. Write a for loop with another counter that retrieves the value of the key that matches your counter.

psuedo

while(has more tokens){
Integer(counter++)
String = next token
Hashmap put(Integer, String)
}

for(int i=0; i<size; i++){
if(i %10 == 0){
Integer key = new Integer(i);
print Hashmap get(key)
}

Something along those lines anyways.

Ray

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 14, 2002 11:44 AM
Reply to this message Reply
Oops !!

for(int i=0; i<size; i++){
if((i % 10) == 0){
System.out.println(i);
}

The println should be

String str = list.get(i);

System.out.println(str);

Sorry about that.

Ray

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: for loop to read in every 10th word Posted: Nov 15, 2002 8:20 AM
Reply to this message Reply
hello daniel ray
thanks for the ideas
do u have any ideas for this? i want to be able to read in my file then i want to be able to test if the string between 2 delimeters is empty or filled with text if it is both still print them out and then i want to loop though the file and put into a hashtable then i want to print out every 1 11 21 31 41 word and so on then i want it to loop though again and print out the 2 22 32 42 word and so on and the same with 3 23 33 and 4 24 34 and so on the problem i get i that when i try to retrive my text from the hashtable it will print out what word it likes is there anway in which you can solve this so that it will orint out the order in which it went into the hashtable
could you adjust my code if u have any solutions please.
thanks for you time
ben

Flat View: This topic has 6 replies on 1 page
Topic: Honesty is the Best Policy Previous Topic   Next Topic Topic: Another complicated qiestion about JVM

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use