473,652 Members | 2,965 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 5895
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(File name));
String line;

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

StringTokenizer token = new StringTokenizer (line);
int number = token.countToke ns();
while(token.has MoreTokens()){
String word = token.nextToken ();
String hold = (reverseFunctio n(word));
StringTokenizer tok = new StringTokenizer (hold);
var += (tok.nextToken( )+ " ");


}
System.out.prin tln(var);
System.out.prin tln(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(File name));
String line;

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

StringTokenizer token = new StringTokenizer (line);
int number = token.countToke ns();
while(token.has MoreTokens()){
String word = token.nextToken ();//Tokenizes the file

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


}
System.out.prin tln(var); //prints out the new sentence
System.out.prin tln(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(File name));
String line;

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

StringTokenizer token = new StringTokenizer (line);
int number = token.countToke ns();
while(token.has MoreTokens()){
String word = token.nextToken ();
String hold = (reverseFunctio n(word));
StringTokenizer tok = new StringTokenizer (hold);
var += (tok.nextToken( )+ " ");


}
System.out.prin tln(var);
System.out.prin tln(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

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

Similar topics

2
3506
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 query in order to look for the presence of one of these tokens, that is "look for a string like 'tokenX' that is preceded by the beginning of the line OR a pipe, AND that is followed
10
2624
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 integers. What is the best standard(!) C++ way to accomplish this? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
6
3603
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 tokens? Does anyone know? Bill
7
2652
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 forever? - Before Sunrise (1995)
4
3033
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 text then #token1# and then #token2#"; string tokens = tokenizer.GetTokens(s, "#"); The contents of the tokens array would then be token1 and token2. Thanks!
0
1296
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){ strcpy(token, token_ptr); strcat(tokens, strcat(token,"\n"));
1
2962
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 their country"; char delims = "#"; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims );
8
3738
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 12 1 15 2 13 3 6 3 9 I need to read the above file and have the following information in the output file In APPLE 0 occurs 1 time, 1 occurs 3 times, 2 occurs 1 time, 3 occurs 3 times
5
3352
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) which considers the delimiters as tokens,too.Can strtok accomplish this requirement?or could you please let me know if there is any other command in C that would carry out this task?
14
2933
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 have not thought of) that the compiler uses to derive useable information. So, this would be a token " ( ) "
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5620
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
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 we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.