Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer Issues

 
jason candelora
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic