Home‎ > ‎Applied Core Java‎ > ‎Basic I/O‎ > ‎

Token File

Problem : How you can token the string in a java program?

Solution :
java.util.StringTokenizer break a string into tokens.

Program :

Step 1 - Coding

Create a text file c:/sunilos/TokenFile.java  and copy below contents.

import java.util.StringTokenizer;

/**
 * A program to token the string from (,).
 */

public class TokenFile {

    public static void main(String[] args) {
        String str = "This is java,Java is object oriented language,Java is guarantee for job";
       
        StringTokenizer stn = new StringTokenizer(str,",");
        String token=null;
        while(stn.hasMoreElements()){
            token = stn.nextToken();
            System.out.println("Token is : " +token);
        }
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TokenFile.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TokenFile.java with help of 
    • javac TokenFile.java command.  Command will createTokenFile .class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TokenFile command.

Output


Token is : This is java
Token is : Java is object oriented language
Token is : Java is guarantee for job

<<Previous | Next>>

ċ
TokenFile.java
(1k)
Mahima Hardia,
Aug 19, 2011, 4:02 AM
Comments