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
14 5895
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 ?
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.
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.
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
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);
}
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.
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 - while((line = Buf.readLine()) != null){
-
String hold = reverseFunction(line);
-
System.out.println(hold);
-
}
-
this is my first asiignment i was kind of exploring different types and methods.. i guess :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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
|
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)
|
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!
| |
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"));
|
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 );
|
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
|
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?
|
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
" ( ) "
|
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,...
| |
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...
|
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,...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |