DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    12

    Unhappy String Tokenizer

    Hello experts,
    I have some text which contains 30 characters that is stored in a variable. I am reading those text using tokenizer. I want to display these 30 characters in 3 lines. 10 characters in each line. How to identify and print each 10 characters. my coding is as follows...

    class file2{

    public static void main(String[] args) throws IOException {

    String st1="You may choose an icon for you";

    StringTokenizer st = new StringTokenizer(st1,"10");
    while (st.hasMoreTokens())
    {
    System.out.println(st.nextToken());
    }

    Thank you

    Balen

  2. #2
    Join Date
    May 2006
    Posts
    23
    Code:
    class file2{
    
    public static void main(String[] args) throws IOException {
    
       String st1="You may choose an icon for you";
       // If you know the string length is constant e.g: 30
       // you don't need an iterator.
       System.out.println(st1.substring(0, 10)); // Returns chars 0 through 9
       System.out.println(st1.substring(10, 20)); // Returns chars 10 through 19
       System.out.println(st1.substring(20)); //Returns chars 20 to end
       
       
       StringTokenizer st = new StringTokenizer(st1,"10");
       while (st.hasMoreTokens()) {
          System.out.println(st.nextToken());
       } 
    }

  3. #3
    Join Date
    Mar 2006
    Posts
    7
    StringTokenizer st = new StringTokenizer("Hello java");
    while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
    }
    prints the following output:

    Hello
    java

    Please see full description here http://www.developerzone.biz/index.p...d=89&Itemid=36
    www.hotdir.biz Hot Java Directory

  4. #4
    Join Date
    Aug 2003
    Posts
    313
    StringTokenizer won't do this for you (unless there is something that I don't know about it), you will need to do it manuall like smcneill suggested. If you don't know its a constant size then you can simply do it in a loop. If you don't want to break the string in the middle of words then you can do something a little bit more complicated with StringTokenizer.

    Code:
    StringTokenizer st = new StringTokenizer(string);
    int curCount = 0;
    String current;
    while( st.hasMoreTokens() ) {
      current = st.nextToken();
      if( curCount + current.length() < 10 || curCount == 0 ) {
        System.out.print(current + " ");
        curCount += current.length() + 1;
      } else {
        System.out.println();
        System.out.print(current + " ");
        curCount = current.length() + 1;
      }
      if( curCount > 10 ) {
        curCount = 0;
        System.out.println();
      }
    }
    Hope this helps.
    ~evlich

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  4. Replies: 0
    Last Post: 06-22-2000, 07:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links