|
Hi all;
I have a problem regarding my loop.The code is below:
StringTokenizer wordFactory = new StringTokenizer(myNewString);
while (wordFactory.hasMoreTokens())
{
//get the next word from the sentence
Aword = wordFactory.nextToken();
long time = System.currentTimeMillis();
}
let me explain how the code works.
the input is "aaa bbb ccc" and it is suppose to print one by one and the output will be printed out one by one, aaa then bbb and then ccc.
i do this method since i want to track the time of each word with brute force method :
for(char r = 'a'; r <= 'z'; r++){
first = r;
for(char s = 'a'; s<='z'; s++){
second = s;
for (char t = 'a'; t <= 'z'; t++){
third = t;
anotherWord=String.valueOf(first)+second+third;
if(anotherWord == Aword){
System.out.print(anotherWord)
//track the time when anotherWord == Aword
}
}
}
}
my problem is, once "aaa" has been detected by the code, how can i proceed to detect "bbb" and "ccc"?
i really appreciate anyone's help. if the problem explanation is quite blur, u can ask me to explain in detail!
thanx in advance!
regards;
dulcinea
|
|
|
hi
if(anotherWord == Aword){
System.out.print(anotherWord)
//track the time when anotherWord == Aword
}
replace with
if(anotherWord .equals(Aword)){
System.out.print(anotherWord);
break;
//track the time when anotherWord == Aword
}
|
|
|
import java.util.StringTokenizer;
public class Test
{
public static void main(String[] args)
{
String input = "aaa bbb ccc";
StringTokenizer tok = new StringTokenizer(input);
while(tok.hasMoreElements()) {
String token = tok.nextToken();
char first, second, third;
String theword;
for(char r = 'a'; r <= 'z'; r++) {
first = r;
for(char s = 'a'; s <= 'z'; s++) {
second = s;
for (char t = 'a'; t <= 'z'; t++) {
third = t;
theword = String.valueOf(first) + second + third;
if(theword.equals(token)) {
System.out.println(theword);
r = (char)'z'+1;
break;
}
}
}
}
}
}
}
|
|
|
|
|
|
|
|
|
// |