Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: count the length of each word in a string

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default count the length of each word in a string

    I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

    hello world how are you
    There are 0 words of length 0
    There are 0 words of length 1
    There are 0 words of length 2
    There are 3 words of length 3
    There are 0 words of length 4
    There are 2 words of length 5

    ithis is my code so far it sort of does the job but not the way i want it too

    import java.util.Scanner;
    import java.util.StringTokenizer;

    public class Brown_Matthew_13117002{

    public static int count(String s, int len){
    int result=0;
    StringTokenizer st=new StringTokenizer(s,"[ ,;]");
    while(st.hasMoreTokens()){
    if(st.nextToken().length()==len){
    result++;
    }
    }
    return result;

    }

    public static void main(String args[])
    {
    Scanner scan = new Scanner(System.in);
    String line ="";
    int max = 6;

    while((line=scan.nextLine())!=null){
    String sentence [] = line.split(" ");
    for(int i=0;i<sentence.length;i++) {

    System.out.println(sentence[i]);

    System.out.println("There are " + count(line, i) + " words of lenghth " + i);


    }
    }
    }
    }

    the output would end up being
    hello
    There are 0 words of lenghth 0
    world
    There are 0 words of lenghth 1
    how
    There are 0 words of lenghth 2
    are
    There are 3 words of lenghth 3
    you
    There are 0 words of lenghth 4

    i have no idea how to fix this any h help is appreciated as i am new to java.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: count the length of each word in a string

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    Crossposted here.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: count the length of each word in a string

    After reading the answers the dreamincode users gave, it seems like you just ignored their advice. What is it that you are trying to fix? Go through your code line by line and trace what the program is doing. Your loops, for example, don't cover every case by a long shot. Please make an effort to fix your own code before simply reposting the question.

Similar Threads

  1. Replies: 1
    Last Post: March 6th, 2014, 06:29 PM
  2. Java word length frequency applet help!
    By tuks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2014, 09:26 AM
  3. word count in a in a file
    By raj4322 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 20th, 2012, 09:54 AM
  4. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  5. Word length frequency help for applet
    By jake6047 in forum Java Applets
    Replies: 5
    Last Post: August 26th, 2011, 07:47 AM

Tags for this Thread