The Artima Developer Community
Sponsored Link

Java Answers Forum
help with a loop to read every 10th word

5 replies on 1 page. Most recent reply: Nov 22, 2002 8:40 AM by jason

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 5 replies on 1 page
jason

Posts: 7
Nickname: bibs
Registered: Nov, 2002

help with a loop to read every 10th word Posted: Nov 21, 2002 1:54 AM
Reply to this message Reply
Advertisement
i have this loop which reads in every 10th word starting from position 1, from the vector but now i want to be able to loop though it again and get every 10th word starting from pstion2 so when i first print it out it prints out words 1, 11, 21, 31 and so on and then i loop though it again to print out words 2, 22, 32, 42 and so on and then 3, 13, 13, and so on could anyone give me any solutions please
thanks jason
Enumeration e = v.elements(); Object o; for(int i = 0;e.hasMoreElements();i++) { o = e.nextElement(); if (i%10 == 0){ System.out.println(o.toString()); }


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: help with a loop to read every 10th word Posted: Nov 21, 2002 4:22 AM
Reply to this message Reply
Jason,

i don't know where you'd wanna stop... therefore this program prints out all until it finds that a number does not have a sibling at the subsequent 10th place. i added 50 elements in the Vector and printed them out as you said. hope this helps.

import java.util.*;

class tryVect
{
public static void main(String[] args)
{
Vector v = new Vector();

for (int i=0;i<50 ;i++ )
{
String temp = i+"";
v.addElement(temp);
}

for (int x = 1; x < v.size()-9 ; x++ )
{
for (int j=x ; j < v.size();j=j+10)
{
System.out.print((String)v.elementAt(j)+" ");
}
System.out.println("");
}


}
}

jason

Posts: 7
Nickname: bibs
Registered: Nov, 2002

Re: help with a loop to read every 10th word Posted: Nov 21, 2002 4:53 AM
Reply to this message Reply
first of all thanks for yr time.
but how would i get that to work with a stringtokenizer and test if there is no text between to delimeters(comma) still printout the empty string and also that idea was great but how would i stop after it has read in the first 10 lines because what it prints out is this
could you please spare some more time and patience please
thanks again
jason
1 11 21 31 41
2 12 22 32 42
3 13 23 33 43
4 14 24 34 44
5 15 25 35 45
6 16 26 36 46
7 17 27 37 47
8 18 28 38 48
9 19 29 39 49
10 20 30 40
and i would want it to stop after here
11 21 31 41
12 22 32 42
13 23 33 43
14 24 34 44
15 25 35 45
16 26 36 46
17 27 37 47
18 28 38 48
19 29 39 49
20 30 40
21 31 41
22 32 42
23 33 43
24 34 44
25 35 45
26 36 46
27 37 47
28 38 48
29 39 49
30 40
31 41
32 42
33 43
34 44
35 45
36 46
37 47
38 48
39 49
40

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: help with a loop to read every 10th word Posted: Nov 22, 2002 3:16 AM
Reply to this message Reply
Jason,

The problem with a StringTokenizer is that after traversing the entire string once, you will have to re-initialize it to traverse it again. That means that once you've read.. say the 50th token and now want to read the 3rd token, you cannot. Since you've already been thro' the 3rd to reach the 50th you cannot go back to it again...until of course you initialize the tokenizer again.

You may try taking the tokens and filling them up in a vector and then traversing it to show the output the way you want.

But if you really need to use the StringTokenizer then here's what you can do:


import java.util.Vector;
import java.util.StringTokenizer;

class tryVect
{
public static void main(String[] args)
{
// This is string you might construct. i have added numbers behind for easy verification. Haven't had time for empty tokens. you can try that i'm sure :)

String mainStr = "The1,request2,goes3,to4,the5,controller6,section7,where8,it9,is10,handled11,by 12,a13,Request14,Intercepting15,Filter.16,This17,servlet18,filter19,receives20,t he21,request22,and23,does24,the25,necessary26,security27,checks.28,It29,then30,p asses31,the32,request33,to34,the35,Front36,Controller37,servlet.38,The39,Front40 ,Controller's41,job42,is43,to44,pull45,out46,data47,from48,the49,request50,form5 1,and52,create53,an54,event55,object56,with57,that58,data.59";

StringTokenizer str = new StringTokenizer(mainStr,",");
int first=1,i = 1;
Object o;
int count = str.countTokens();

// This is the main loop

while (str.hasMoreTokens() && i<=10) // will run only 10 times.
{
str = new StringTokenizer(mainStr, ",");
int temp = 1;

/*
As the StringTokenizer begins from 1 every time, this help the loop to omit token 1 when it should begin with 2 and so..on. Try without this loop also.
*/

while (i > 1)
{
o = str.nextElement();
if (++temp == i)
{
break;
}
}

// here is where you traverse thro' the entire thing for 10th position tokens.

for (int j = i;j < count ;j++)
{
o = str.nextElement();
if (((j-first) == 10) || (j == first))
{
System.out.print(o.toString()+" ");
first = j;
}

}

System.out.println("");

// increase counters

i++;
first = i;
}
}
}

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: help with a loop to read every 10th word Posted: Nov 22, 2002 3:16 AM
Reply to this message Reply
Jason,

The problem with a StringTokenizer is that after traversing the entire string once, you will have to re-initialize it to traverse it again. That means that once you've read.. say the 50th token and now want to read the 3rd token, you cannot. Since you've already been thro' the 3rd to reach the 50th you cannot go back to it again...until of course you initialize the tokenizer again.

You may try taking the tokens and filling them up in a vector and then traversing it to show the output the way you want.

But if you really need to use the StringTokenizer then here's what you can do:


import java.util.Vector;
import java.util.StringTokenizer;

class tryVect
{
public static void main(String[] args)
{
// This is string you might construct. i have added numbers behind for easy verification. Haven't had time for empty tokens. you can try that i'm sure :)

String mainStr = "The1,request2,goes3,to4,the5,controller6,section7,where8,it9,is10,handled11,by 12,a13,Request14,Intercepting15,Filter.16,This17,servlet18,filter19,receives20,t he21,request22,and23,does24,the25,necessary26,security27,checks.28,It29,then30,p asses31,the32,request33,to34,the35,Front36,Controller37,servlet.38,The39,Front40 ,Controller's41,job42,is43,to44,pull45,out46,data47,from48,the49,request50,form5 1,and52,create53,an54,event55,object56,with57,that58,data.59";

StringTokenizer str = new StringTokenizer(mainStr,",");
int first=1,i = 1;
Object o;
int count = str.countTokens();

// This is the main loop

while (str.hasMoreTokens() && i<=10) // will run only 10 times.
{
str = new StringTokenizer(mainStr, ",");
int temp = 1;

/*
As the StringTokenizer begins from 1 every time, this help the loop to omit token 1 when it should begin with 2 and so..on. Try without this loop also.
*/

while (i > 1)
{
o = str.nextElement();
if (++temp == i)
{
break;
}
}

// here is where you traverse thro' the entire thing for 10th position tokens.

for (int j = i;j < count ;j++)
{
o = str.nextElement();
if (((j-first) == 10) || (j == first))
{
System.out.print(o.toString()+" ");
first = j;
}

}

System.out.println("");

// increase counters

i++;
first = i;
}
}
}

jason

Posts: 7
Nickname: bibs
Registered: Nov, 2002

Re: help with a loop to read every 10th word Posted: Nov 22, 2002 8:40 AM
Reply to this message Reply
hello thanks alot for yr help i am very gratefull
it helped me quite a bit thanks
jason

Flat View: This topic has 5 replies on 1 page
Topic: Convert long date to byte array Previous Topic   Next Topic Topic: removing duplicates from arrays

Sponsored Links



Google
  Web Artima.com   

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