Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,297 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 851 people online right now. Registration is fast and FREE... Join Now!




word count

 
Reply to this topicStart new topic

word count

sereesha
18 Dec, 2008 - 09:48 PM
Post #1

New D.I.C Head
*

Joined: 16 Dec, 2008
Posts: 4

Hi

i have a trouble in this code.i want the postion of the unique word occured in several times.



CODE

import java.io.*;
import java.util.StringTokenizer;

public class  WordCount4{
  private static void linecount(String fName, BufferedReader in) throws IOException{

  long numLine=0;
  long numWords = 0;
  String line;
    do{
      line = in.readLine();
      if (line != null){
        numWords += wordcount(line);
        numLine++;
      }
    }while(line != null);
  
    System.out.println("Number of words: " + numWords);
    System.out.println("Number of Lines: " + numLine);
    
  }

  private static long wordcount(String line){
  long numWords = 0;
  /*  int index = 0;
    boolean prevWhiteSpace = true;
    while(index < line.length()){
      char c = line.charAt(index++);
      boolean currWhiteSpace = Character.isWhitespace(c);
      if(prevWhiteSpace && !currWhiteSpace){
        numWords++;
      }
      prevWhiteSpace = currWhiteSpace;
    }*/
    return numWords;
  }
  public static void main(String[] args){
    
    long numLine=0;
    String line;
    try{
      if (args.length == 0)
      {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        line = in.readLine();
        String reqword="is";
        int reqWordCount=0;
        StringTokenizer st = new StringTokenizer(line," ");
        int count = 0;
        int charcount=-1;
        while(st.hasMoreElements()){
            String word = st.nextToken();
                count++;
                charcount=charcount+word.length()+1;
                if(word.equalsIgnoreCase(reqword))
                {  
                    reqWordCount++;
                    
                    
                    System.out.println("pos"+(charcount-reqword.length()));
                }
                
            //System.out.println("word:"+word+" Char Count:"+word.length()+" Word No:"+count);
                
        }
        
        System.out.println("The word 'is' occures "+reqWordCount+" times");
      }
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }
}



User is offlineProfile CardPM
+Quote Post


markhazlett9
RE: Word Count
18 Dec, 2008 - 10:21 PM
Post #2

Student: Will Code for Food!
Group Icon

Joined: 12 Jul, 2008
Posts: 1,145



Thanked: 32 times
Dream Kudos: 25
My Contributions
QUOTE(sereesha @ 18 Dec, 2008 - 09:48 PM) *

Hi

i have a trouble in this code.i want the postion of the unique word occured in several times.



CODE

import java.io.*;
import java.util.StringTokenizer;

public class  WordCount4{
  private static void linecount(String fName, BufferedReader in) throws IOException{

  long numLine=0;
  long numWords = 0;
  String line;
    do{
      line = in.readLine();
      if (line != null){
        numWords += wordcount(line);
        numLine++;
      }
    }while(line != null);
  
    System.out.println("Number of words: " + numWords);
    System.out.println("Number of Lines: " + numLine);
    
  }

  private static long wordcount(String line){
  long numWords = 0;
  /*  int index = 0;
    boolean prevWhiteSpace = true;
    while(index < line.length()){
      char c = line.charAt(index++);
      boolean currWhiteSpace = Character.isWhitespace(c);
      if(prevWhiteSpace && !currWhiteSpace){
        numWords++;
      }
      prevWhiteSpace = currWhiteSpace;
    }*/
    return numWords;
  }
  public static void main(String[] args){
    
    long numLine=0;
    String line;
    try{
      if (args.length == 0)
      {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        line = in.readLine();
        String reqword="is";
        int reqWordCount=0;
        StringTokenizer st = new StringTokenizer(line," ");
        int count = 0;
        int charcount=-1;
        while(st.hasMoreElements()){
            String word = st.nextToken();
                count++;
                charcount=charcount+word.length()+1;
                if(word.equalsIgnoreCase(reqword))
                {  
                    reqWordCount++;
                    
                    
                    System.out.println("pos"+(charcount-reqword.length()));
                }
                
            //System.out.println("word:"+word+" Char Count:"+word.length()+" Word No:"+count);
                
        }
        
        System.out.println("The word 'is' occures "+reqWordCount+" times");
      }
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }
}





Can you clarify what you want with your code please? I don't understand...
User is offlineProfile CardPM
+Quote Post

sereesha
RE: Word Count
19 Dec, 2008 - 12:56 AM
Post #3

New D.I.C Head
*

Joined: 16 Dec, 2008
Posts: 4

QUOTE(markhazlett9 @ 18 Dec, 2008 - 10:21 PM) *

QUOTE(sereesha @ 18 Dec, 2008 - 09:48 PM) *

Hi

i have a trouble in this code.i want the postion of the unique word occured in several times.



CODE

import java.io.*;
import java.util.StringTokenizer;

public class  WordCount4{
  private static void linecount(String fName, BufferedReader in) throws IOException{

  long numLine=0;
  long numWords = 0;
  String line;
    do{
      line = in.readLine();
      if (line != null){
        numWords += wordcount(line);
        numLine++;
      }
    }while(line != null);
  
    System.out.println("Number of words: " + numWords);
    System.out.println("Number of Lines: " + numLine);
    
  }

  private static long wordcount(String line){
  long numWords = 0;
  /*  int index = 0;
    boolean prevWhiteSpace = true;
    while(index < line.length()){
      char c = line.charAt(index++);
      boolean currWhiteSpace = Character.isWhitespace(c);
      if(prevWhiteSpace && !currWhiteSpace){
        numWords++;
      }
      prevWhiteSpace = currWhiteSpace;
    }*/
    return numWords;
  }
  public static void main(String[] args){
    
    long numLine=0;
    String line;
    try{
      if (args.length == 0)
      {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        line = in.readLine();
        String reqword="is";
        int reqWordCount=0;
        StringTokenizer st = new StringTokenizer(line," ");
        int count = 0;
        int charcount=-1;
        while(st.hasMoreElements()){
            String word = st.nextToken();
                count++;
                charcount=charcount+word.length()+1;
                if(word.equalsIgnoreCase(reqword))
                {  
                    reqWordCount++;
                    
                    
                    System.out.println("pos"+(charcount-reqword.length()));
                }
                
            //System.out.println("word:"+word+" Char Count:"+word.length()+" Word No:"+count);
                
        }
        
        System.out.println("The word 'is' occures "+reqWordCount+" times");
      }
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }
}





Can you clarify what you want with your code please? I don't understand...

Hi

actually here i am counting the word occurrence in a line.but i have to count the word in the text which is given and also in that code it is not taking 2 whitespaces as count only it is taking 1 whitespace.And also i have to state the position of that unique word which is occurred at several times.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:45PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month