By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

tokens to string

P: 16
I am a newbie at Java. I am doing an assignment on string manipulations. Can anyone give me tips on how to make tokens back into a sentence.
Eg.
Good ------>(to) Good Morning Everyone!!
Morning
Everyone!!
thanks in advance
Jan 16 '07 #1
Share this Question
Share on Google+
14 Replies


10K+
P: 13,264
I am a newbie at Java. I am doing an assignment on string manipulations. Can anyone give me tips on how to make tokens back into a sentence.
Eg.
Good ------>(to) Good Morning Everyone!!
Morning
Everyone!!
thanks in advance
Do you have the words/tokens in an array or in a StringTokenizer?
Jan 16 '07 #2

P: 16
StringTokenizer.
Jan 16 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
So you have a StringTokenizer whose contents are "Good", "Morning", and "Everyone!"

Make a new String variable set to the empty string "". While your Tokenizer has more tokens, append the nextToken to the end of your String, plus a space.
Jan 16 '07 #4

10K+
P: 13,264
So you have a StringTokenizer whose contents are "Good", "Morning", and "Everyone!"

Make a new String variable set to the empty string "". While your Tokenizer has more tokens, append the nextToken to the end of your String, plus a space.
Yes try that and post your code (using code tags) if you get any problem.
Jan 16 '07 #5

P: 16
I tried the code it's working but there is a problem..
I tokenized the sentences from a file
Hello World
Good Morning Everyone!!
I was suppose to reverse each word and output the result: right now i am
getting
olloH dlroW dooG gninroM !!enoyrevE ( how do i make this into 2 different lines like in the file)?
try{
BufferedReader Buf = new BufferedReader(new FileReader(Filename));
String line;

while((line = Buf.readLine()) != null){

StringTokenizer token = new StringTokenizer(line);
int number = token.countTokens();
while(token.hasMoreTokens()){
String word = token.nextToken();
String hold = (reverseFunction(word));
StringTokenizer tok = new StringTokenizer(hold);
var += (tok.nextToken()+ " ");


}
System.out.println(var);
System.out.println(number);


}
Thanks for the help
Jan 16 '07 #6

P: 16
sorry i forgot the documentation part
try{
//reads from file
BufferedReader Buf = new BufferedReader(new FileReader(Filename));
String line;

while((line = Buf.readLine()) != null){

StringTokenizer token = new StringTokenizer(line);
int number = token.countTokens();
while(token.hasMoreTokens()){
String word = token.nextToken();//Tokenizes the file

String hold = (reverseFunction(word));// calls a function to reverse words//
StringTokenizer tok = new StringTokenizer(hold);
var += (tok.nextToken()+ " "); //combines the word to form the sentence


}
System.out.println(var); //prints out the new sentence
System.out.println(number);


}
Jan 16 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
If your input file has the words in two seperate lines, you can repeat the following process for each line:

1) Get the next line from your input file.
2) Make a StringTokenizer with the input string.
3) Reverse each token and hold in a larger string variable.
4) Output the larger string variable

This will reverse each word in each line individually, rather than the whole file.
Jan 16 '07 #8

10K+
P: 13,264
I tried the code it's working but there is a problem..
I tokenized the sentences from a file
Hello World
Good Morning Everyone!!
I was suppose to reverse each word and output the result: right now i am
getting
olloH dlroW dooG gninroM !!enoyrevE ( how do i make this into 2 different lines like in the file)?
try{
BufferedReader Buf = new BufferedReader(new FileReader(Filename));
String line;

while((line = Buf.readLine()) != null){

StringTokenizer token = new StringTokenizer(line);
int number = token.countTokens();
while(token.hasMoreTokens()){
String word = token.nextToken();
String hold = (reverseFunction(word));
StringTokenizer tok = new StringTokenizer(hold);
var += (tok.nextToken()+ " ");


}
System.out.println(var);
System.out.println(number);


}
Thanks for the help
I don't see why you are going through the trouble of the tokenizer. Why not just

Expand|Select|Wrap|Line Numbers
  1.  while((line = Buf.readLine()) != null){ 
  2.       String hold = reverseFunction(line);
  3.       System.out.println(hold);
  4. }
  5.  
Jan 16 '07 #9

P: 16
this is my first asiignment i was kind of exploring different types and methods.. i guess :)
Jan 16 '07 #10

P: 16
when i try reversing like you said by passing the string line to reverse function it is even reversing the string.
Eg: Hello World! ---> !dlroW olleH
but i want ----> olleH !dlorW
Jan 16 '07 #11

Ganon11
Expert 2.5K+
P: 3,652
My suggestion should still work - have you tried that yet?
Jan 16 '07 #12

10K+
P: 13,264
when i try reversing like you said by passing the string line to reverse function it is even reversing the string.
Eg: Hello World! ---> !dlroW olleH
but i want ----> olleH !dlorW
I see what the fuss is about then. Generally to tokenize a string, you should use the string.split() method
Jan 16 '07 #13

P: 16
yes i tried your method too.. it is giving me the same result. I guess i would have to make some changes in the reverseFunction for your method ro work.I'll keep trying by finding what string.split() does and by changing reverseFunction around :)
thanks
Jan 16 '07 #14

10K+
P: 13,264
yes i tried your method too.. it is giving me the same result. I guess i would have to make some changes in the reverseFunction for your method ro work.I'll keep trying by finding what string.split() does and by changing reverseFunction around :)
thanks
No you don't need to change your reverse function.

Expand|Select|Wrap|Line Numbers
  1.  String[] tokens = line.split("\\s");
then reverse each token and append as Ganon has shown you how to
Jan 16 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.