#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Java program to count number of letters of each word entered


    /*The assignment is to count the number of one letter words, two letter words etc. in text entered by user. I added in a few extra things in the code. I still didn't get it. Please help.*/




    package javaapplication78;

    import java.io.*;
    //import java.util.Scanner;

    public class Main {


    // public static void main(String[] args) {

    private static void linecount(String fName, BufferedReader in) throws IOException{
    // System.out.println("Enter a Sentence\n:");
    //Scanner Sentence=new Scanner(System.in);
    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();


    }}}
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Hi, and welcome to Dev Shed.

    I'd suggest having a look at the how to post a question thread: you haven't really provided enough information for us to help you properly.

    Firstly, it makes your code easier to read if you enclose it within [code][/code] tags.

    And you haven't said what the actual problem is! Is your code doing something it shouldn't? Or is it failing to do something it should be doing? Or does it not run at all? In any event, were there any error messages that would help identify the problem?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    java application to summarize length of words entered


    As stated , the application is supposed to accept text from the user and display the number of occurrences of each word length found. example: number of one letter words, two letter words and so on.

    My code has no errors. It's just that it does not do what it is supoosed to. I put in a line count and a word count to guide me. I tried to count the length of each word but end up getting the length of all the text that I have entered along with the line count and word count.



    Code:
    package javaapplication78;
    
    import java.io.*;
    //import java.util.Scanner;
    
    public class Main {
    
    
    // public static void main(String[] args) {
    
    private static void linecount(String fName, BufferedReader in) throws IOException{
    // System.out.println("Enter a Sentence\n:");
    //Scanner Sentence=new Scanner(System.in);
    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();
    
    
    }}}
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    String tokenizer


    Originally Posted by jadeaa54345
    As stated , the application is supposed to accept text from the user and display the number of occurrences of each word length found. example: number of one letter words, two letter words and so on.

    My code has no errors. It's just that it does not do what it is supoosed to. I put in a line count and a word count to guide me. I tried to count the length of each word but end up getting the length of all the text that I have entered along with the line count and word count.



    Code:
    package javaapplication78;
    
    import java.io.*;
    //import java.util.Scanner;
    
    public class Main {
    
    
    // public static void main(String[] args) {
    
    private static void linecount(String fName, BufferedReader in) throws IOException{
    // System.out.println("Enter a Sentence\n:");
    //Scanner Sentence=new Scanner(System.in);
    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();
    
    
    }}}
    Are you allowed to use string tokenizer? That will separate your "words" (well, it will cut off whenever there is a space.) Look up how to use those and hopefully they help you!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0
    Originally Posted by blkhockeypro19
    Are you allowed to use string tokenizer? That will separate your "words" (well, it will cut off whenever there is a space.) Look up how to use those and hopefully they help you!
    Thanks I will look it up and find out from my lecturer.
  10. #6
  11. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    StringTokenizer vs String.split()


    Using StringTokenizer to tokenise words is a very "old" way of accomplishing the task. Perhaps you should consider using the split() method in the String class. This method accepts a regular expression which splits the text into words.

    The regular expression for splitting words is simply "\\s+". Then, the method would return a string array and you may operate on the string array to categorise words as single character, 2 characters etc..
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo