• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringTokenizer Issues

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic