hi! i really need a java word counting program. . . .

an example input:

input=> hello there!, hi!
output=> hello = 2
there = 1

i hope you guys can help! i really need this! thanks in advance! by the way it should run under command prompt! thanks!

Sorry, we're here to help you learn how to program in java. If you have code that you are having problems with, please post it with your questions and we'll help you get your code to work.

import java.util.Scanner;
 public class Part
 {
   public static void main(String args[])
   {
        //String searchFor = "is";
        //String searchFor = "to";
        String searchFor="blank";
        //String base = "This is object oriented programming language.is is my favourite.";
        //String base = " to be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer";
        String base;
        int len = searchFor.length();
        int result = 0;
        Scanner input = new Scanner(System.in);
        base = input.nextLine();
        Scanner scanner = new Scanner(System.in);
        searchFor = scanner.nextLine();



        if (len > 0) 
        { 
             int start = base.indexOf(searchFor);
             while (start != -1)
             {
                int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String.
                int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String.
                //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not.
                if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
                {
                    //Not to be incremented as string searchFor is a part of a base.
                }
                else
                    result++;//Update the result.
                start = base.indexOf(searchFor, (start+len));
             }
         }
        //System.out.println(result);
        System.out.println("the sentence Contains: "+result+" of the word: to");



    }
 }

so far, thats the code. it will only work if base and searchFor contains a value already, but i want to have a user input and every word should be counted. i've already spend days for this. i hope you can help me of this.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You only need to define one Scanner object to read in the user's input. You can use the same one to read more than one line from the user.
Can you document what the user is supposed to enter? You need to print out a message to the user telling him what he should enter before you read it.

/Check if the Character ... is an Alphabet

The Character class has methods to do this.

You need to spend more time thinking about the steps your program must take to solve the problem.
What do you need to do first? => Ask the user a question
Then next => Get the user's response
what next?
And then what?
and then what?

i'm really new on java, I've just started last week, and my mind is already twisting. i really need your help guys!.

import java.util.Scanner;
 public class Part
 {
   public static void main(String args[])
   {

        String searchFor="blank";

        String base = "";
        int len = searchFor.length();
        int result = 0;
        String blank = " ";

        System.out.printf("\t\tenter string: ");
        Scanner input = new Scanner(System.in);

        base = input.nextLine();
        base = blank + base;

        System.out.printf("\n\t\tenter what you want to search: ");
        Scanner scanner = new Scanner(System.in);
        searchFor = scanner.nextLine();




        if (len > 0) 
        { 
             int start = base.indexOf(searchFor);
             while (start != -1)
             {
                int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String.
                int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String.
                //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not.
                if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
                {
                    //Not to be incremented as string searchFor is a part of a base.
                }
                else
                    result++;//Update the result.
                start = base.indexOf(searchFor, (start+len));
             }
         }
        //System.out.println(result);
        System.out.println("\n\t\tThe sentence Contains: "+result+" of the word/s: " +searchFor);



    }
 }

this is the progress . . . it can only count until 2 words any more than two, i'll be getting an "outofBound" error . . .

Edited 3 Years Ago by Reverend Jim: Fixed formatting

if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
This is really ugly code and not readable. What does it do?
If you are testing the character use the Character class method.
Or if you must, use the char value itself, for example: 'a' and 'z' vs some number that no one knows the value of.

it can only count until 2 words

Please explain the limit? I don't see any arrays.

Do you know about the String class's split method?

This article has been dead for over six months. Start a new discussion instead.