Results 1 to 9 of 9
Thread: Count Words
- 07-17-2011, 06:56 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
- 07-17-2011, 08:05 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
We don't write code for you here. Make an effort and we will help(hint: a Map<String, Integer> will be very helpful).
- 07-17-2011, 08:42 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
This 's my code. but it 's not right.
I want output:
example:
Enter string: I am a student a student not good.
I: 1
am: 1
a: 2
Student: 2
not: 1
good: 1
Java Code:package lab7; import java.io.*; public class Main{ private static void linecount(String fName, BufferedReader in) throws IOException{ long numChar = 0; long numLine=0; long numWords = 0; String line; do{ line = in.readLine(); if (line != null){ numChar += line.length(); numWords += wordcount(line); numLine++; } }while(line != null); System.out.println("File Name: " + fName); System.out.println("Number of characters: " + numChar); System.out.println("Number of words: " + numWords); System.out.println("Number of Lines: " + numLine); } private static void linecount(String fileName){ BufferedReader in = null; try{ FileReader fileReader = new FileReader(fileName); in = new BufferedReader(fileReader); linecount(fileName,in); } catch(IOException e){ e.printStackTrace(); } } 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 numChar = 0; long numLine=0; String line; try{ if (args.length == 0) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); line = in.readLine(); numChar = line.length(); if (numChar != 0){ numLine=1; } System.out.println("Number of characters: " + numChar); System.out.println("Number of words: " + wordcount(line)); System.out.println("Number of lines: " + numLine); }else{ for(int i = 0; i < args.length; i++){ linecount(args[i]); } } } catch(IOException e){ e.printStackTrace(); } } }
- 07-17-2011, 08:46 PM #4
I don't see how your code is supposed to do anything to what you asked. This doesn't keep track of what words have been used at all...
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-17-2011, 08:48 PM #5
What kind of techniques and/or classes do you know how to use to be able to remember the counts?count the number of times each word occurs in that string
- 07-17-2011, 09:17 PM #6
Cross posted at Llist of words and frequency of each word
- 07-18-2011, 01:12 AM #7
- 07-18-2011, 11:50 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Use as sunde887 suggested Map<String, Integer> .It s the best and easiest way to do it. Try it out.
- 07-18-2011, 01:24 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 11
- Rep Power
- 0
Hi,
1) Counts the No of Words in a String
2) Place each of those words in a buffer (means any Array, List r some Storage)
3) Iterate through that Buffer Starting with the first word
4) Count the occurrence of the word
just think like a student solving a problem ......... u can easily build upon the logic
Similar Threads
-
Read text file and count words.
By chdn202002 in forum New To JavaReplies: 5Last Post: 07-17-2011, 02:44 PM -
Help: Num to words
By MyOnlineChurva in forum New To JavaReplies: 16Last Post: 01-08-2010, 01:41 AM -
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02-04-2009, 08:55 PM -
Printing out ... depending on character count of previous words entered.
By TheRocket in forum New To JavaReplies: 5Last Post: 11-22-2008, 01:34 PM -
help w words
By Gilgamesh in forum New To JavaReplies: 5Last Post: 11-21-2007, 06:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks