Results 1 to 6 of 6

Thread: Breaking a String into Words In Java

  1. #1
    Join Date
    Jan 2009
    Posts
    66

    Breaking a String into Words In Java

    Hello, I want to know the details about the one program which will help me to get the solution for Breaking a String into Words In Java. I have tried it, but I am not able to get the solution for it. If you are having logic of the developing this code, then also reply me, so I can able to get the solution to achieve it.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Breaking a String into Words In Java

    Hello, If you want to achieve this program of Breaking a String into Words In Java then you must need to make use of the methods given below in your program. It will be help to create a code regarding it. So, just make use of the code and then simply you will able to get the solution for your program.
    • StringTokenizer(String str)
    • hasMoreTokens()
    • nextToken()

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Breaking a String into Words In Java

    Hello, I have got the code below which if you add in your program then you will able to get the solution for your requirement of the Breaking a String into Words In Java. So, just make use of it.
    Code:
    String Stokenizerr = "This is Techarena Forum";
    StringTokenizer Stokenizer = new StringTokenizer(Stokenizerr);
    while (Stokenizer.hasMoreTokens()) 
    {
      println(Stokenizer.nextToken());
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Breaking a String into Words In Java

    Hello, I have created the below mentioned code for your program for the breaking the string into words:
    Code:
    String str="";
    int num=0;
    int num2=fullName.indexOf(",");
    if (num2>=0)
    {
    for(int num3=0;num3<num2-1;num3=num3+1)
    {
    str=str+fullName.charAt(num3);
    num=num3;
    }
    firstName=str;
    str=""; 
    num=num+2;
    for(int t=num;t<fullName.length();t=t+1)
    {
    str=str+fullName.charAt(t);
    }
    lName=str;
    }
    else
    {
    String[] f=fullName.split(" ");
    firstName=f[0];
    lName=f[1];
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Breaking a String into Words In Java

    Hello, If you make use of the code below which uses the split function then you will able to get the solution for your problem:
    Code:
    String str = "This is a Sentence of me.";  
    String[] BWord = str.split(" ");  
    for (String word : BWord)  
    {  
    System.out.println(word);  
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Breaking a String into Words In Java

    Hello, If you make use of the code below in java then you will able to Break a String into Words In Java:
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Breaking
    {
      String string;
      int total = 0;
      public static void main(String[] args) 
    {
        Breaking BReadereaking = new Breaking();
      }
      public Breaking()
    {
        try
    {
          InputStreamReader IStream = new InputStreamReader(System.in);
          BufferedReader BReader = new BufferedReader(IStream);
          System.out.print("Enter String: ");
          string = BReader.readLine();
          System.out.println("Enter String is : "+ string);
          System.out.println("Breaking this stringing is : ");
          StringTokenizer token = new StringTokenizer(string);
          while (token.hasMoreTokens()){
            total++;
            string = token.nextToken();
            System.out.println(string);
          }
          System.out.println("Number of Words : "+ total);
        }
        catch(IOException e)
    {
    }
      }
    }

Similar Threads

  1. Definition of a String in Java?
    By cloud101 in forum Software Development
    Replies: 5
    Last Post: 28-07-2014, 10:14 AM
  2. The String Class in Java
    By blueprats in forum Guides & Tutorials
    Replies: 3
    Last Post: 12-03-2010, 02:44 PM
  3. Highlighting Words in a JTextComponent in Java
    By Aakarshan.d in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:38 PM
  4. Search the presence of n words in a string
    By cobra2008 in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 09:50 PM
  5. Recovery of a string between two words
    By !const in forum Software Development
    Replies: 5
    Last Post: 16-04-2009, 08:38 PM

Tags for this Thread

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
  •  
Page generated in 1,714,151,014.45263 seconds with 16 queries