File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer Issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer Issues" Watch "StringTokenizer Issues" New topic
Author

StringTokenizer Issues

jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Hi,
I want to take a string such as "Hello" and convert that to a string that looks like this: "h + e + l + l + o" . This is the loop that I am using to try to accomplish this feat:
import javax.swing.*;
import java.text.*;
import java.util.StringTokenizer;
public class romanNumeral
//Naming the class and declaring it to be public
{//begin class
public static void main(String[] args)

{//begin method
String input = "hello";

StringTokenizer tokenizer = new StringTokenizer (input);

int stringLength = input.length();

String word = " ";

String digit;

while (tokenizer.hasMoreTokens())

{//begin while

digit = tokenizer.nextToken();

if (stringLength == 1)

{//begin if

word = word + " " + digit;

}//end if

else

{//begin else

word = word + digit + " + " + stringLength;

}//end else

stringLength = stringLength - 1;

}//end while

JOptionPane.showMessageDialog(null,

"Your converted string is " + word,

"The Results",

JOptionPane.INFORMATION_MESSAGE);

}//end method

}//end class
This code returns "hello +".
I know that this is because the delimiter is not set right but I am not sure how to tell it to set each character as its own token as opposed to each word.
Please help!
Jason
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Hi Jason,
I think u've just complicated the whole thing. The simplest thing to do would be smthg like this.
public class VinClass{
public static void main(String args[]){
String input = "hello";
String output = "";
for(int i = 0; i < input.length(); i++)
output = output + input.charAt(i) + "+";
System.out.println(output);
}
}
hope this helps.
Cheers,
Vinod
Originally posted by jason candelora:
Hi,
I want to take a string such as "Hello" and convert that to a string that looks like this: "h + e + l + l + o" . This is the loop that I am using to try to accomplish this feat:
import javax.swing.*;
import java.text.*;
import java.util.StringTokenizer;
public class romanNumeral
//Naming the class and declaring it to be public
{//begin class
public static void main(String[] args)

{//begin method
String input = "hello";

StringTokenizer tokenizer = new StringTokenizer (input);

int stringLength = input.length();

String word = " ";

String digit;

while (tokenizer.hasMoreTokens())

{//begin while

digit = tokenizer.nextToken();

if (stringLength == 1)

{//begin if

word = word + " " + digit;

}//end if

else

{//begin else

word = word + digit + " + " + stringLength;

}//end else

stringLength = stringLength - 1;

}//end while

JOptionPane.showMessageDialog(null,

"Your converted string is " + word,

"The Results",

JOptionPane.INFORMATION_MESSAGE);

}//end method

}//end class
This code returns "hello +".
I know that this is because the delimiter is not set right but I am not sure how to tell it to set each character as its own token as opposed to each word.
Please help!
Jason
jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Thanks for the help!
I ended up using a combination of both of our solutions to solve the problem...I really appreciate the insight!
Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer Issues
 
Similar Threads
print till the next blank line
June Newsletter Puzzle
Java Static Problems
LinkedList of objects
urgent help! my while loop is not working