473,503 Members | 479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tokens to string

16 New Member
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
14 5884
r035198x
13,262 MVP
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
hello12
16 New Member
StringTokenizer.
Jan 16 '07 #3
Ganon11
3,652 Recognized Expert Specialist
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
r035198x
13,262 MVP
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
hello12
16 New Member
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
hello12
16 New Member
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
3,652 Recognized Expert Specialist
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
r035198x
13,262 MVP
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
hello12
16 New Member
this is my first asiignment i was kind of exploring different types and methods.. i guess :)
Jan 16 '07 #10
hello12
16 New Member
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
3,652 Recognized Expert Specialist
My suggestion should still work - have you tried that yet?
Jan 16 '07 #12
r035198x
13,262 MVP
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
hello12
16 New Member
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
r035198x
13,262 MVP
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

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

Similar topics

2
3495
by: Aurelien Mazurie | last post by:
Hello to all, I'm trying to do a weird (?) thing with regexp and MySQL. For example, i have a column with: token1|token2|token3 (three tokens separated by pipes) I'm trying to write an SQL...
10
2609
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned...
6
3595
by: Bill Cunningham | last post by:
Lexical generators such as Bison, Flex, Lex, &c produce C tokens for the parser or compiler. What do these C tokens look like? According to ANSI C, what does the standard have to say about C...
7
2641
by: Steven Woody | last post by:
in C, is there any function can be used to decompose tokens from a string? if not, can i find it in CPP? thanks! -- steven woody (id: narke) Celine: Well, who says relationships have to last...
4
3022
by: Jay Nesbitt | last post by:
Is there a quick way to extract tokens from a string of text? I know Java provides a StringTokenizer class to do this. Is there something similar in the dot net framework? Ex. string s = "some...
0
1279
by: kamal9 | last post by:
#include <stdio.h> #include <string.h> void string2Lines(char line, char tokens){ char *token_ptr, token; char *i=";" " " "," "." "?" "!"; token_ptr = strtok(line, i ); while(token_ptr){...
1
2953
by: kara18 | last post by:
Hi, After splitting a string in to tokens using strtok ,how can I get the tokens in to different arrays. for example char str = "now # is the time for all # good men to come to the # aid of...
8
3730
by: shivam001 | last post by:
I have the following file as the input APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126 ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128 MANGO 0 136 1 143 2 143 3 143 4 136 BANANA 0 5 1...
5
3340
by: gpaps87 | last post by:
hi, i wanted to know whether we can use strtok command to mark delimiters as tokens as well.In Java,we have a command: StringTokennizer(String str, String delimiters, boolean delimAsToken) ...
14
2918
by: mdh | last post by:
Hi all, From p 125, gives rise to this issue for me. Is it true that a "token" in C ( philisophically ) is the least amount of digits/chars/underscores/*s ( and other non blank space that I...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.